QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#65911 | #3047. Wind of Change | gyh20 | ML | 0ms | 0kb | C++14 | 3.6kb | 2022-12-04 12:02:44 | 2022-12-04 12:02:47 |
Judging History
answer
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
int n,c1,c2,h1[400002],h2[400002],dfn[200002],siz[200002],son[200002],top[200002],dep[200002],fa[200002],tim,stk[200002],tp,mn,Z,pos,Siz[200002];
char v[200002],tg[200002];
long long ans[200002],dis[200002],f1[200002],f2[200002],g[200002],Dis[200002];
struct edge{int to,next,w;}e1[400002],e2[400002];
inline void add1(re int x,re int y,re int z){e1[++c1]=(edge){y,h1[x],z},h1[x]=c1;}
inline void add2(re int x,re int y,re int z){e2[++c2]=(edge){y,h2[x],z},h2[x]=c2;}
inline void dfs(re int x,re int y){
Siz[x]=1,dep[x]=dep[y]+1,fa[x]=y;
for(re int i=h2[x];i;i=e2[i].next)
if(e2[i].to^y){
Dis[e2[i].to]=Dis[x]+e2[i].w,dfs(e2[i].to,x),Siz[x]+=Siz[e2[i].to];
if(Siz[e2[i].to]>Siz[son[x]])son[x]=e2[i].to;
}
}
inline void dfs1(re int x,re int y){
top[x]=y,dfn[x]=++tim;
if(son[x])dfs1(son[x],y);
for(re int i=h2[x];i;i=e2[i].next)
if(e2[i].to^fa[x]&&e2[i].to^son[x])
dfs1(e2[i].to,e2[i].to);
}
inline bool in(re int x,re int y){return dfn[y]>=dfn[x]&&dfn[y]<dfn[x]+Siz[x];}
inline int lca(re int x,re int y){
while(top[x]^top[y]){
if(dep[top[x]]>dep[top[y]])x=fa[top[x]];
else y=fa[top[y]];
}return dep[x]<dep[y]?x:y;
}
vector<int>H[200002];
inline void Build(vector<int>&tmp){
tp=0;
for(re int i=0;i<tmp.size();++i){
if(i==0)stk[++tp]=tmp[i];
else{
while(tp>1&&!in(stk[tp-1],tmp[i]))H[stk[tp-1]].push_back(stk[tp]),--tp;
if(in(stk[tp],tmp[i]))stk[++tp]=tmp[i];
else{
re int z=lca(stk[tp],tmp[i]);
H[z].push_back(stk[tp]),--tp;
if(z^stk[tp])stk[++tp]=z;
stk[++tp]=tmp[i];
}
}
}
for(re int i=1;i<tp;++i)H[stk[i]].push_back(stk[i+1]);
}
vector<int>O;
inline void Dfs(re int x,re int y){
siz[x]=1;re int mx=0;
for(re int i=h1[x];i;i=e1[i].next)
if(e1[i].to^y&&!v[e1[i].to])
Dfs(e1[i].to,x),mx=max(mx,siz[e1[i].to]),siz[x]+=siz[e1[i].to];
mx=max(mx,Z-siz[x]);
if(mx<mn)mn=mx,pos=x;
}
inline void Dfs1(re int x,re int y){
O.push_back(x),siz[x]=1;
for(re int i=h1[x];i;i=e1[i].next)
if(e1[i].to^y&&!v[e1[i].to])
dis[e1[i].to]=dis[x]+e1[i].w,Dfs1(e1[i].to,x),siz[x]+=siz[e1[i].to];
}
inline void dfs1(re int x){
f1[x]=tg[x]?dis[x]:1e18,f2[x]=g[x]=1e18;
for(auto z:H[x]){
dfs1(z);
if(f1[z]+Dis[z]-Dis[x]<f1[x])f2[x]=f1[x],f1[x]=f1[z]+Dis[z]-Dis[x];
else if(f1[z]+Dis[z]-Dis[x]<f2[x])f2[x]=f1[z]+Dis[z]-Dis[x];
}
if(tg[x])ans[x]=min(ans[x],(f1[x]==dis[x]?f2[x]:f1[x])+dis[x]);
}
inline void dfs2(re int x){
if(tg[x])ans[x]=min(ans[x],g[x]+dis[x]),g[x]=min(g[x],dis[x]);
for(auto z:H[x]){
if(f1[z]+Dis[z]-Dis[x]==f1[x])g[z]=min(g[x],f2[x])+Dis[z]-Dis[x];
else g[z]=min(g[x],f1[x])+Dis[z]-Dis[x];
dfs2(z);
}tg[x]=0,H[x].clear();
}
inline bool cmp(re int x,re int y){return dfn[x]<dfn[y];}
inline void dfz(re int x,re int y){
if(y==1)return;
mn=1e9,Z=y,Dfs(x,x),x=pos,dis[x]=0,Dfs1(x,x),v[x]=1;
for(auto z:O)tg[z]=1;
O.push_back(1),sort(O.begin(),O.end(),cmp),O.resize(unique(O.begin(),O.end())-O.begin()),Build(O);
dfs1(1),dfs2(1),O.clear();
for(re int i=h1[x];i;i=e1[i].next)
if(!v[e1[i].to])
dfz(e1[i].to,siz[e1[i].to]);
}
int main(){
n=read();
for(re int i=1;i<=n;++i)ans[i]=1e18;
for(re int i=1,x,y,z;i<n;++i)x=read(),y=read(),z=read(),add1(x,y,z),add1(y,x,z);
for(re int i=1,x,y,z;i<n;++i)x=read(),y=read(),z=read(),add2(x,y,z),add2(y,x,z);
dfs(1,1),dfs1(1,1),dfz(1,n);
for(re int i=1;i<=n;++i)printf("%lld\n",ans[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory 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...