QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#261942 | #3047. Wind of Change | lmeowdn | TL | 0ms | 0kb | C++14 | 4.3kb | 2023-11-23 13:52:44 | 2023-11-23 13:52:45 |
answer
//vanitas vanitatum et omnia vanitas
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=2.5e5+5,inf=0x3f3f3f3f3f3f3f3f;
int n,dfn[N],son[N],sz1[N],sz2[N],pa[N],tick,top[N],ans[N],d[N],b[N],
vst[N],rt,m,msz[N],p[N],pk,q[N],qk,r1[N],r2[N],fm[N],fs[N],fp[N],g[N];
vp e1[N],e2[N]; vi t2[N];
void dfs21(int u,int fa) {
for(auto [v,w]:e2[u]) if(v!=fa) {
d[v]=d[u]+1, r2[v]=r2[u]+w;
dfs21(v,u); sz2[u]+=sz2[v]; pa[v]=u;
if(sz2[v]>sz2[son[u]]) son[u]=v;
} sz2[u]++;
}
void dfs22(int u,int tp) {
top[u]=tp; dfn[u]=++tick; if(son[u]) dfs22(son[u],tp);
for(auto [v,w]:e2[u]) if(v!=pa[u]&&v!=son[u]) dfs22(v,v);
}
void dfs23(int u) {
fm[u]=fs[u]=inf, fp[u]=0;
for(int v:t2[u]) {
dfs23(v);
if(g[v]<fm[u]) fs[u]=fm[u], fp[u]=v, fm[u]=g[v];
else if(g[v]<fs[u]) fs[u]=g[v];
}
chmin(g[u],fm[u]);
//cout<<u<<" "<<g[u]<<" "<<fm[u]<<' '<<fp[u]<<" "<<fs[u]<<endl;
}
void dfs24(int u,int mn) {
if(b[u]) {
chmin(ans[u],r1[u]+mn+r2[u]);
chmin(ans[u],r1[u]+fm[u]-r2[u]);
}
for(int v:t2[u]) {
int vm=mn; if(b[u]) chmin(vm,r1[u]-r2[u]);
if(v==fp[u]) chmin(vm,fs[u]-2*r2[u]);
else chmin(vm,fm[u]-2*r2[u]);
dfs24(v,vm);
}
}
int lca(int u,int v) {
for(;top[u]!=top[v];u=pa[top[u]])
if(d[top[u]]<d[top[v]]) swap(u,v);
return d[u]<d[v]?u:v;
}
bool cmp(const int &x,const int &y) {
return dfn[x]<dfn[y];
}
void work1() {
//cout<<"WORK: "; rep(i,1,pk) cout<<p[i]<<" "; puts("");
sort(p+1,p+pk+1,cmp); static int st[N];
qk=1, q[qk]=1; int top=1; st[1]=1;
rep(i,1,pk) if(p[i]!=1) {
int x=p[i], y=lca(x,st[top]);
if(y==st[top]) st[++top]=x, q[++qk]=x;
else {
while(top>1&&d[st[top-1]]>d[y]) t2[st[top-1]].eb(st[top]), top--;
t2[y].eb(st[top]), top--;
if(st[top]!=y) st[++top]=y, q[++qk]=y;
st[++top]=x, q[++qk]=x;
}
} while(top>1) t2[st[top-1]].eb(st[top]), top--;
//cout<<"Q: "; rep(i,1,qk) cout<<q[i]<<" "; puts("");
rep(i,1,qk) g[q[i]]=inf, b[q[i]]=0;
rep(i,1,pk) g[p[i]]=r2[p[i]]+r1[p[i]], b[p[i]]=1;
//rep(i,1,n) cout<<g[i]<<" "; puts("");
//rep(i,1,n) cout<<b[i]<<" "; puts("");
dfs23(1); dfs24(1,inf);
rep(i,1,qk) {vi emp; t2[q[i]].swap(emp);}
}
void dfs11(int u,int fa) {
sz1[u]=1;
for(auto [v,w]:e1[u]) if(v!=fa&&!vst[v]) {
dfs11(v,u); sz1[u]+=sz1[v];
chmax(msz[u],sz1[v]);
} chmax(msz[u],m-sz1[u]);
if(msz[u]<msz[rt]) rt=u;
}
void dfs12(int u,int fa) {
p[++pk]=u; sz1[u]=1;
for(auto [v,w]:e1[u]) if(v!=fa&&!vst[v]) {
r1[v]=r1[u]+w; dfs12(v,u); sz1[u]+=sz1[v];
}
}
void dfs13(int u,int tm) {
rt=0, msz[0]=n+1, m=tm, dfs11(u,0), u=rt;
r1[u]=0, pk=0, vst[u]=1, dfs12(u,0), work1();
for(auto [v,w]:e1[u]) if(!vst[v]) dfs13(v,sz1[v]);
}
signed main() {
n=read(); rep(i,1,n) ans[i]=inf;
rep(i,2,n) {
int u=read(), v=read(), w=read();
e1[u].eb(v,w), e1[v].eb(u,w);
}
rep(i,2,n) {
int u=read(), v=read(), w=read();
e2[u].eb(v,w), e2[v].eb(u,w);
}
dfs21(1,0); dfs22(1,0); dfs13(1,n);
rep(i,1,n) printf("%lld\n",ans[i]);
return 0;
}
詳細信息
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...