QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#54888#3047. Wind of ChangeXiejiadongRE 0ms0kbC++1.5kb2022-10-11 11:00:512022-10-11 11:00:55

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-11 11:00:55]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-10-11 11:00:51]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int n,x,y,z;
ll d[N],mn[N],ans[N];
vector<int>tmp;
struct tree{
	int o,sz[N],mx[N],rt,vis[N],fa[N];
	vector<pair<int,int> >v[N];
	vector<pair<int,ll> >q[N];
	void getrt(int x,int fa,int tot){
		sz[x]=1,mx[x]=0;
		for(auto p:v[x]){
			int y=p.first;
			if(!vis[y]&&y!=fa) getrt(y,x,tot),sz[x]+=sz[y],mx[x]=max(mx[x],sz[y]);
		}
		if((mx[x]=max(mx[x],tot-sz[x]))<mx[rt]) rt=x;
	}
	void getd(int x,int fa,int rt){
		if(o) q[rt].push_back({x,d[x]});
		else q[x].push_back({rt,d[x]});
		for(auto p:v[x]){
			int y=p.first;
			if(!vis[y]&&y!=fa) d[y]=d[x]+p.second,getd(y,x,rt);
		}
	}
	void div(int x){
		vis[x]=1,d[x]=0,getd(x,0,x);
		for(auto p:v[x]){
			int y=p.first;
			if(!vis[y]) rt=0,getrt(y,x,sz[y]),fa[rt]=x,div(rt);
		}
	}
	void build(){
		for(int i=1;i<n;i++)
			scanf("%d%d%d",&x,&y,&z),
			v[x].push_back({y,z}),v[y].push_back({x,z}); 
		mx[0]=1e9,getrt(1,0,n),div(rt);
	}
}t1,t2;
void calc(int l1){
	tmp.clear();
	for(auto i:t1.q[l1]){
		int x=i.first;
		for(auto j:t2.q[x]){
			int l2=j.first; ll d=i.second+j.second;
			if(~mn[l2]) ans[x]=min(ans[x],mn[l2]+d),mn[l2]=min(mn[l2],d);
			else mn[l2]=d,tmp.push_back(l2);
		}
	}
	for(int i:tmp) mn[i]=-1;
}
signed main(){
	scanf("%d",&n),t1.o=1,t1.build(),t2.build();
	for(int i=1;i<=n;i++) ans[i]=1e18,mn[i]=-1;
	for(int i=1;i<=n;i++)
		calc(i),reverse(t1.q[i].begin(),t1.q[i].end()),calc(i);
	for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
	return 0;
}

详细

Test #1:

score: 0
Runtime Error

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:


result: