QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#40061 | #3047. Wind of Change | Yudanjun | TL | 0ms | 0kb | C++20 | 2.8kb | 2022-07-16 00:36:49 | 2022-07-16 00:36:49 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
#define N 250010
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
while(c<48){if(c=='-')f=0;c=getchar();}
while(c>=48) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?x:-x;
}
int n;
ll res[N],d1[N][20],d2[N][20];
unordered_map<ll,ll> mp;
struct Tree2{
int rt,fa[N],dep[N];
vector<int> g[N];
void dfs(int u){
for(auto v:g[u]){
dep[v]=dep[u]+1;fa[v]=u;
dfs(v);
}
}
} T1,T2;
struct Tree1{
ll d[N];
int dep[N],fa[N][20],vis[N],dp[N],sz[N],rt,sum;
vector<pair<int,int> > g[N];
void init(){
for(int i=1;i<n;i++){
int u=read(),v=read(),w=read();
g[u].push_back({v,w});
g[v].push_back({u,w});
}
}
void dfs(int u,int f){
dep[u]=dep[f]+1;fa[u][0]=f;
for(int i=1;i<20;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for(auto i:g[u]){
int v=i.first,w=i.second;
if(v==f) continue;
d[v]=d[u]+w;dfs(v,u);
}
}
int LCA(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=19;i>=0;i--)
if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if(x==y) return x;
for(int i=19;i>=0;i--)
if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
ll dist(int u,int v){
return d[u]+d[v]-2*d[LCA(u,v)];
}
void getroot(int u,int fa){
dp[u]=0;sz[u]=1;
for(auto i:g[u]){
int v=i.first,w=i.second;
if(v==fa||vis[v]) continue;
getroot(v,u);
sz[u]+=sz[v];dp[u]=max(dp[u],sz[v]);
}
dp[u]=max(dp[u],sum-sz[u]);
if(dp[u]<dp[rt]) rt=u;
}
void solve(int u,bool flg){
vis[u]=1;
for(auto i:g[u]){
int v=i.first,w=i.second;
if(vis[v]) continue;
sum=sz[v];rt=0;
getroot(v,u);
if(!flg) T1.g[u].push_back(rt);
else T2.g[u].push_back(rt);
solve(rt,flg);
}
}
void Solve(bool flg){
rt=0;dp[0]=sum=n;getroot(1,0);
if(!flg) T1.rt=rt;
else T2.rt=rt;
solve(rt,flg);
}
} S1,S2;
signed main(){
n=read();
S1.init();S2.init();
S1.dfs(1,0);S2.dfs(1,0);
S1.Solve(0);S2.Solve(1);
T1.dfs(T1.rt);T2.dfs(T2.rt);
for(int i=1;i<=n;i++){
for(int u=i;u;u=T1.fa[u]) d1[i][T1.dep[u]]=S1.dist(i,u);
for(int u=i;u;u=T2.fa[u]) d2[i][T2.dep[u]]=S2.dist(i,u);
}
memset(res,63,sizeof(res));
for(int i=1;i<=n;i++){
for(int u=i;u;u=T1.fa[u])
for(int v=i;v;v=T2.fa[v]){
ll hsh=1ll*u*N+v,dist=d1[i][T1.dep[u]]+d2[i][T2.dep[v]];
if(!mp.count(hsh)) mp[hsh]=dist;
else{
ll tmp=mp[hsh];
res[i]=min(res[i],dist+tmp);
mp[hsh]=min(tmp,dist);
}
}
}
mp.clear();
for(int i=n;i;i--){
for(int u=i;u;u=T1.fa[u])
for(int v=i;v;v=T2.fa[v]){
ll hsh=1ll*u*N+v,dist=d1[i][T1.dep[u]]+d2[i][T2.dep[v]];
if(!mp.count(hsh)) mp[hsh]=dist;
else{
ll tmp=mp[hsh];
res[i]=min(res[i],dist+tmp);
mp[hsh]=min(tmp,dist);
}
}
}
for(int i=1;i<=n;i++) printf("%lld\n",res[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
250000 137466 116241 624409157 188961 163586 148491970 13958 122239 46409636 186120 44010 919858866 72320 102560 92679302 158544 236582 882613876 22331 114267 646741536 210782 42861 679450428 56693 45397 898958944 71188 114724 904191407 15203 225401 210626781 31071 144225 278121829 110354 83972 4557...