QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#357408 | #3047. Wind of Change | Harry27182 | TL | 0ms | 0kb | C++14 | 2.9kb | 2024-03-18 21:04:09 | 2024-03-18 21:04:10 |
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,u,v,w,ans[250005];vector<int>now;
struct Tree
{
struct edge{int v,w,nxt;}e[500005];
int cnt,h[250005],siz[250005],mx[250005],rt,S,p[250005];
int dep[250005],f[250005][20],vis[250005];vector<int>g[250005];
void add(int u,int v,int w){e[++cnt]={v,w,h[u]};h[u]=cnt;}
void find(int u,int fa)
{
siz[u]=1;mx[u]=0;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa||vis[v])continue;
find(v,u);siz[u]+=siz[v];mx[u]=max(mx[u],siz[v]);
}
mx[u]=max(mx[u],S-siz[u]);
if(mx[u]<mx[rt])rt=u;
}
int get(int u,int fa)
{
int res=1;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa||vis[v])continue;
res+=get(v,u);
}
return res;
}
void solve(int u)
{
vis[u]=1;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(vis[v])continue;
mx[rt=0]=0x3f3f3f3f;S=get(v,u);find(v,u);
p[rt]=u;g[u].emplace_back(rt);solve(rt);
}
}
void main()
{
mx[rt=0]=0x3f3f3f3f;S=n;find(1,0);solve(rt);
}
void dfs(int u)
{
now.emplace_back(u);
for(int i=0;i<g[u].size();i++)dfs(g[u][i]);
}
void init(int u,int fa)
{
f[u][0]=fa;
for(int i=1;(1<<i)<=n;i++)f[u][i]=f[f[u][i-1]][i-1];
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa)continue;
dep[v]=dep[u]+e[i].w;init(v,u);
}
}
int lca(int u,int v)
{
if(dep[u]<dep[v])swap(u,v);
for(int i=18;i>=0;i--)if(dep[f[u][i]]>=dep[v])u=f[u][i];
if(u==v)return u;
for(int i=18;i>=0;i--)if(f[u][i]!=f[v][i])u=f[u][i],v=f[v][i];
return f[u][0];
}
int dis(int u,int v){return dep[u]+dep[v]-2*dep[lca(u,v)];}
}T1,T2;
struct node{int w,x;}mn[250005][2];
bool operator ==(node x,node y){return x.w==y.w&&x.x==y.x;}
bool operator <(node x,node y){return x.w<y.w;}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for(int i=1;i<n;i++)
{
cin>>u>>v>>w;
T1.add(u,v,w);T1.add(v,u,w);
}
for(int i=1;i<n;i++)
{
cin>>u>>v>>w;
T2.add(u,v,w);T2.add(v,u,w);
}
T1.init(1,0);T2.init(1,0);
T1.main();T2.main();
for(int i=1;i<=n;i++)mn[i][0]=mn[i][1]={0x3f3f3f3f3f3f3f3f,0},ans[i]=0x3f3f3f3f3f3f3f3f;
for(int i=1;i<=n;i++)
{
now.clear();now.shrink_to_fit();
T1.dfs(i);
for(int j=0;j<now.size();j++)
{
int x=now[j];
for(int k=x;k;k=T2.p[k])
{
node now={T1.dis(x,i)+T2.dis(x,k),x};
if(now<mn[k][0])mn[k][1]=mn[k][0],mn[k][0]=now;
else if(now<mn[k][1])mn[k][1]=now;
}
}
for(int j=0;j<now.size();j++)
{
int x=now[j];
for(int k=x;k;k=T2.p[k])
{
node now={T1.dis(x,i)+T2.dis(x,k),x};
if(mn[k][0]==now)ans[x]=min(ans[x],mn[k][1].w+now.w);
else ans[x]=min(ans[x],mn[k][0].w+now.w);
}
}
for(int j=0;j<now.size();j++)
{
int x=now[j];
for(int k=x;k;k=T2.p[k])mn[k][0]=mn[k][1]={0x3f3f3f3f3f3f3f3f,0};
}
}
for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
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...