結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-14 11:11:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,672 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 213,816 KB
実行使用メモリ 58,464 KB
最終ジャッジ日時 2024-02-16 18:04:25
合計ジャッジ時間 25,028 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 401 ms
49,484 KB
testcase_01 AC 378 ms
49,484 KB
testcase_02 AC 380 ms
49,484 KB
testcase_03 AC 384 ms
49,484 KB
testcase_04 AC 385 ms
49,484 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 323 ms
50,380 KB
testcase_09 AC 316 ms
50,380 KB
testcase_10 AC 331 ms
50,380 KB
testcase_11 AC 340 ms
50,380 KB
testcase_12 AC 338 ms
50,252 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 275 ms
48,136 KB
testcase_17 AC 161 ms
29,240 KB
testcase_18 AC 216 ms
35,328 KB
testcase_19 AC 247 ms
42,916 KB
testcase_20 AC 80 ms
16,636 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 335 ms
52,064 KB
testcase_25 AC 340 ms
50,784 KB
testcase_26 AC 335 ms
52,448 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 100 ms
18,048 KB
testcase_44 AC 72 ms
13,056 KB
testcase_45 AC 281 ms
42,376 KB
testcase_46 AC 182 ms
28,800 KB
testcase_47 AC 318 ms
47,092 KB
testcase_48 AC 345 ms
49,484 KB
testcase_49 AC 358 ms
49,484 KB
testcase_50 AC 344 ms
49,484 KB
testcase_51 AC 354 ms
49,484 KB
testcase_52 AC 357 ms
49,484 KB
testcase_53 AC 4 ms
6,676 KB
testcase_54 AC 3 ms
6,676 KB
testcase_55 AC 3 ms
6,676 KB
testcase_56 AC 3 ms
6,676 KB
testcase_57 AC 3 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 359 ms
49,484 KB
testcase_61 AC 363 ms
49,484 KB
testcase_62 AC 358 ms
49,484 KB
testcase_63 AC 316 ms
48,772 KB
testcase_64 AC 206 ms
32,952 KB
testcase_65 AC 291 ms
46,124 KB
testcase_66 AC 89 ms
16,000 KB
testcase_67 AC 71 ms
13,824 KB
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
権限があれば一括ダウンロードができます

ソースコード

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<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> order(N);
	rep(i,0,N) order[i]=i;
	sort(all(order),[&](int l,int r){
		return A[l]>A[r];
	});
	int L=0,R=0;
	int X=order[0],Y=order[0],D=0;
	while(L<N){
		while(R<N&&order[L]==order[R]){
			int tmpX=dist(X,order[R]);
			int tmpY=dist(Y,order[R]);
			if(tmpX<tmpY) swap(X,Y),swap(tmpX,tmpY);
			if(D<tmpX) D=tmpX,Y=order[R];
			R++;
		}
		rep(i,L,R){
			chmax(ans[order[i]],dist(order[i],X));
			chmax(ans[order[i]],dist(order[i],Y));
		}
		L=R;
	}

	rep(i,0,N){
		if(i) cout<<" ";
		cout<<ans[i];
	}
	cout<<"\n";
}
0