QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#40083#3047. Wind of ChangeYudanjunTL 0ms0kbC++204.7kb2022-07-16 09:35:222022-07-16 09:35:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-16 09:35:23]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2022-07-16 09:35:22]
  • 提交

answer

#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("avx","sse2")
#include<bits/stdc++.h>
#define ll long long
#define N 250010
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(c<48){if(c=='-')f=0;c=getchar();}
	while(c>=48) x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f?x:-x;
}
int n;
ll res[N],d1[N][20],d2[N][20];
unordered_map<ll,ll> mp[N];
struct Tree2{
	int rt,fa[N],dep[N];
	vector<int> g[N];
	void dfs(int u){
		for(auto v:g[u]){
			dep[v]=dep[u]+1;fa[v]=u;
			assert(dep[v]<20);
			dfs(v);
		}
	}
} T1,T2;
struct Tree1{
	ll d[N];
	int dep[N],fa[N][20],vis[N],dp[N],sz[N],rt,sum;
	vector<pair<int,int> > g[N];
	void init(){
		for(int i=1;i<n;i++){
			int u=read(),v=read(),w=read();
			g[u].push_back({v,w});
			g[v].push_back({u,w});
		}
	}
	void dfs(int u,int f){
		dep[u]=dep[f]+1;fa[u][0]=f;
		for(int i=1;i<20;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
		for(auto i:g[u]){
			int v=i.first,w=i.second;
			if(v==f) continue;
			d[v]=d[u]+w;dfs(v,u);
		}
	}
	int LCA(int x,int y){
		if(dep[x]<dep[y]) swap(x,y);
		for(int i=19;i>=0;i--)
			if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
		if(x==y) return x;
		for(int i=19;i>=0;i--)
			if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
		return fa[x][0];
	}
	ll dist(int u,int v){
		return d[u]+d[v]-2*d[LCA(u,v)];
	}
	void getroot(int u,int fa){
		dp[u]=0;sz[u]=1;
		for(auto i:g[u]){
			int v=i.first,w=i.second;
			if(v==fa||vis[v]) continue;
			getroot(v,u);
			sz[u]+=sz[v];dp[u]=max(dp[u],sz[v]);
		}
		dp[u]=max(dp[u],sum-sz[u]);
		if(dp[u]<dp[rt]) rt=u;
	}
	void solve(int u,bool flg){
		vis[u]=1;
		for(auto i:g[u]){
			int v=i.first,w=i.second;
			if(vis[v]) continue;
			sum=sz[v];rt=0;
			getroot(v,u);
			if(!flg) T1.g[u].push_back(rt);
			else T2.g[u].push_back(rt);
			solve(rt,flg);
		}
	}
	void Solve(bool flg){
		rt=0;dp[0]=sum=n;getroot(1,0);
		if(!flg) T1.rt=rt;
		else T2.rt=rt;
		solve(rt,flg);
	}
} S1,S2;
signed main(){
	n=read();
	S1.init();S2.init();
	S1.dfs(1,0);S2.dfs(1,0);
	S1.Solve(0);S2.Solve(1);
	T1.dfs(T1.rt);T2.dfs(T2.rt);
	for(int i=1;i<=n;i++){
		for(int u=i;u;u=T1.fa[u]) d1[i][T1.dep[u]]=S1.dist(i,u);
		for(int u=i;u;u=T2.fa[u]) d2[i][T2.dep[u]]=S2.dist(i,u);
	}
	memset(res,63,sizeof(res));
	for(int i=1;i<=n;i++){
		for(int u=i;u;u=T1.fa[u])
			for(int v=i;v;v=T2.fa[v]){
				ll dist=d1[i][T1.dep[u]]+d2[i][T2.dep[v]];
				ll& val=mp[u][v];
				if(!val) val=dist+1;
				else{
					res[i]=min(res[i],dist+val-1);
					if(dist+1<val) val=dist+1;
				}
			}
	}
	for(int i=1;i<=n;i++) mp[i].clear();
	for(int i=n;i;i--){
		for(int u=i;u;u=T1.fa[u])
			for(int v=i;v;v=T2.fa[v]){
				ll dist=d1[i][T1.dep[u]]+d2[i][T2.dep[v]];
				ll& val=mp[u][v];
				if(!val) val=dist+1;
				else{
					res[i]=min(res[i],dist+val-1);
					if(dist+1<val) val=dist+1;
				}
			}
	}
	for(int i=1;i<=n;i++) printf("%lld\n",res[i]);
	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...

output:


result: