QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#357432 | #3047. Wind of Change | Harry27182 | WA | 3818ms | 196656kb | C++14 | 3.0kb | 2024-03-18 21:12:42 | 2024-03-18 21:12:43 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,u,v,w,ans[250005],dis[105][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 u=1;u<=n;u++)
{
for(int x=u,o=0;x;x=T2.p[x],o++)dis[o][u]=T2.dis(u,x);
}
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],d1=T1.dis(x,i);
for(int k=x,o=0;k;k=T2.p[k],o++)
{
node now={d1+dis[o][x],x};assert(o<=100);
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],d1=T1.dis(x,i);
for(int k=x,o=0;k;k=T2.p[k],o++)
{
node now={d1+dis[o][x],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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 3818ms
memory: 196656kb
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...
output:
41101981722 33898052757 17386222407 47190371137 31165890951 23032562047 36469313934 10929203519 9150749133 49915742961 30769535878 26129987591 35182629851 17070234497 30945920063 24201049200 22946884015 21178918565 24113819218 39824179894 25295838334 22901166222 18834316224 30203983119 42411806191 2...
result:
wrong answer 2nd lines differ - expected: '24783524958', found: '33898052757'