結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-13 16:02:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 532 ms / 3,000 ms
コード長 1,832 bytes
コンパイル時間 2,371 ms
コンパイル使用メモリ 215,364 KB
実行使用メモリ 58,480 KB
最終ジャッジ日時 2024-02-16 18:03:59
合計ジャッジ時間 32,137 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 510 ms
50,284 KB
testcase_01 AC 499 ms
50,284 KB
testcase_02 AC 494 ms
50,284 KB
testcase_03 AC 488 ms
50,284 KB
testcase_04 AC 532 ms
50,284 KB
testcase_05 AC 479 ms
50,284 KB
testcase_06 AC 482 ms
50,284 KB
testcase_07 AC 500 ms
50,284 KB
testcase_08 AC 466 ms
51,180 KB
testcase_09 AC 453 ms
51,180 KB
testcase_10 AC 474 ms
51,180 KB
testcase_11 AC 473 ms
51,052 KB
testcase_12 AC 468 ms
51,052 KB
testcase_13 AC 445 ms
51,180 KB
testcase_14 AC 464 ms
51,052 KB
testcase_15 AC 447 ms
51,180 KB
testcase_16 AC 373 ms
48,128 KB
testcase_17 AC 215 ms
29,244 KB
testcase_18 AC 268 ms
35,312 KB
testcase_19 AC 330 ms
42,956 KB
testcase_20 AC 95 ms
16,700 KB
testcase_21 AC 454 ms
55,152 KB
testcase_22 AC 443 ms
57,456 KB
testcase_23 AC 445 ms
58,480 KB
testcase_24 AC 466 ms
51,952 KB
testcase_25 AC 471 ms
50,800 KB
testcase_26 AC 473 ms
52,464 KB
testcase_27 AC 458 ms
50,800 KB
testcase_28 AC 470 ms
51,824 KB
testcase_29 AC 464 ms
51,184 KB
testcase_30 AC 487 ms
50,284 KB
testcase_31 AC 502 ms
50,284 KB
testcase_32 AC 485 ms
50,284 KB
testcase_33 AC 289 ms
36,908 KB
testcase_34 AC 56 ms
9,984 KB
testcase_35 AC 168 ms
23,168 KB
testcase_36 AC 92 ms
14,592 KB
testcase_37 AC 200 ms
26,496 KB
testcase_38 AC 4 ms
6,676 KB
testcase_39 AC 5 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 3 ms
6,676 KB
testcase_42 AC 3 ms
6,676 KB
testcase_43 AC 137 ms
18,304 KB
testcase_44 AC 87 ms
13,184 KB
testcase_45 AC 404 ms
43,064 KB
testcase_46 AC 255 ms
29,264 KB
testcase_47 AC 479 ms
47,804 KB
testcase_48 AC 505 ms
50,284 KB
testcase_49 AC 501 ms
50,284 KB
testcase_50 AC 511 ms
50,284 KB
testcase_51 AC 498 ms
50,284 KB
testcase_52 AC 495 ms
50,284 KB
testcase_53 AC 3 ms
6,676 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 3 ms
6,676 KB
testcase_56 AC 3 ms
6,676 KB
testcase_57 AC 4 ms
6,676 KB
testcase_58 AC 1 ms
6,676 KB
testcase_59 AC 1 ms
6,676 KB
testcase_60 AC 529 ms
50,284 KB
testcase_61 AC 530 ms
50,284 KB
testcase_62 AC 501 ms
50,284 KB
testcase_63 AC 437 ms
49,532 KB
testcase_64 AC 293 ms
33,464 KB
testcase_65 AC 424 ms
46,876 KB
testcase_66 AC 109 ms
16,256 KB
testcase_67 AC 88 ms
14,080 KB
testcase_68 AC 444 ms
51,004 KB
testcase_69 AC 440 ms
51,004 KB
testcase_70 AC 448 ms
51,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(p) p.begin(),p.end()
void chmin(int &a,int b){a=min(a,b);}
void chmax(int &a,int b){a=max(a,b);}

int main(){
	int N;
	cin>>N;
	vector<int> A(N);
	rep(i,0,N) cin>>A[i];
	vector<int> order(N);
	rep(i,0,N) order[i]=i;
	sort(all(order),[&](int l,int r){
		return A[l]>A[r];
	});
	vector<vector<int>> G(N);
	rep(i,0,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	int B=0;
	while((1<<B)<N) B++;
	B++;
	vector<int> ans(N);
	vector<int> depth(N,-1);
	vector<int> E(N*2);
	vector table(B,vector<int>(N*2));
	int ind=0;
	auto dfs=[&](auto self,int var) -> void {
		E[var]=ind++;
		for(auto x:G[var]){
			if(depth[x]==-1){
				depth[x]=depth[var]+1;
				self(self,x);
			}
		}
		E[var+N]=ind++;
	};
	depth[0]=0;
	dfs(dfs,0);
	rep(i,0,N){
		table[0][E[i]]=depth[i];
		table[0][E[i+N]]=depth[i]-1;
	}
	rep(i,0,B-1) rep(j,0,N*2){
		table[i+1][j]=table[i][j];
		if(j+(1<<i)<N*2) chmin(table[i+1][j],table[i][j+(1<<i)]);
	}
	auto dist=[&](int a,int b) -> int {
		if(E[a]>E[b]) swap(a,b);
		int l=E[a];
		int r=E[b]+1;
		int z=31-__builtin_clz(r-l);
		return depth[a]+depth[b]-2*min(table[z][l],table[z][r-(1<<z)]);
	};
	
	vector<int> X(3);
	auto f=[&](int l1,int r1,int l2,int r2) -> void {
		X[0]=order[l1];
		rep(i,0,2){
			int tmp=-1,tmp2=0;
			rep(j,l1,r1){
				tmp2=dist(order[j],X[i]);
				if(tmp<tmp2) tmp=tmp2,X[i+1]=order[j];
			}
		}
		rep(i,l2,r2) rep(j,1,3) chmax(ans[order[i]],dist(order[i],X[j]));
	};
	
	int L=0,R=0;
	while(L<N){
		while(R!=N&&A[order[L]]==A[order[R]]) R++;
		f(L,R,L,R);
		L=R;
	}
	
	rep(d,0,B-1){
		L=0;
		int M;
		while(L<N){
			M=min(N,L+(1<<d));
			R=min(N,M+(1<<d));
			f(L,M,M,R);
			L=R;
		}
	}
	rep(i,0,N){
		if(i) cout<<" ";
		cout<<ans[i];
	}
	cout<<"\n";
}
0