結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-15 22:05:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 3,000 ms
コード長 1,650 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 213,092 KB
実行使用メモリ 31,848 KB
最終ジャッジ日時 2024-02-16 18:06:21
合計ジャッジ時間 22,096 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 292 ms
22,096 KB
testcase_01 AC 302 ms
22,096 KB
testcase_02 AC 305 ms
22,096 KB
testcase_03 AC 310 ms
22,096 KB
testcase_04 AC 292 ms
22,096 KB
testcase_05 AC 290 ms
22,096 KB
testcase_06 AC 276 ms
22,096 KB
testcase_07 AC 291 ms
22,096 KB
testcase_08 AC 251 ms
22,992 KB
testcase_09 AC 258 ms
23,044 KB
testcase_10 AC 257 ms
23,028 KB
testcase_11 AC 264 ms
22,960 KB
testcase_12 AC 263 ms
22,932 KB
testcase_13 AC 258 ms
23,060 KB
testcase_14 AC 277 ms
22,940 KB
testcase_15 AC 250 ms
23,012 KB
testcase_16 AC 256 ms
26,788 KB
testcase_17 AC 125 ms
16,896 KB
testcase_18 AC 163 ms
19,712 KB
testcase_19 AC 185 ms
24,240 KB
testcase_20 AC 58 ms
11,008 KB
testcase_21 AC 267 ms
28,520 KB
testcase_22 AC 278 ms
30,852 KB
testcase_23 AC 271 ms
31,848 KB
testcase_24 AC 281 ms
25,408 KB
testcase_25 AC 281 ms
24,180 KB
testcase_26 AC 284 ms
25,820 KB
testcase_27 AC 274 ms
24,152 KB
testcase_28 AC 294 ms
25,244 KB
testcase_29 AC 274 ms
24,592 KB
testcase_30 AC 284 ms
22,204 KB
testcase_31 AC 282 ms
22,228 KB
testcase_32 AC 281 ms
22,232 KB
testcase_33 AC 188 ms
16,868 KB
testcase_34 AC 33 ms
6,676 KB
testcase_35 AC 93 ms
11,648 KB
testcase_36 AC 52 ms
8,192 KB
testcase_37 AC 111 ms
12,928 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 3 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 79 ms
9,600 KB
testcase_44 AC 52 ms
7,552 KB
testcase_45 AC 235 ms
19,396 KB
testcase_46 AC 149 ms
14,208 KB
testcase_47 AC 270 ms
21,260 KB
testcase_48 AC 288 ms
22,228 KB
testcase_49 AC 275 ms
22,224 KB
testcase_50 AC 270 ms
22,216 KB
testcase_51 AC 281 ms
22,212 KB
testcase_52 AC 272 ms
22,224 KB
testcase_53 AC 3 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 278 ms
22,096 KB
testcase_61 AC 280 ms
22,096 KB
testcase_62 AC 276 ms
22,096 KB
testcase_63 AC 225 ms
22,312 KB
testcase_64 AC 148 ms
16,080 KB
testcase_65 AC 226 ms
21,208 KB
testcase_66 AC 61 ms
8,960 KB
testcase_67 AC 49 ms
8,064 KB
testcase_68 AC 228 ms
22,852 KB
testcase_69 AC 219 ms
22,852 KB
testcase_70 AC 239 ms
22,852 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(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	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);
	}
	vector<int> ans(N);
	vector<int> depth(N,-1);
	vector<int> E(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);

	vector<int> seg(N*4);
	rep(i,0,N){
		seg[2*N+E[i]]=depth[i];
		seg[2*N+E[i+N]]=depth[i]-1;
	}
	for(int i=2*N-1;i>0;i--) seg[i]=min(seg[2*i],seg[2*i+1]);
	auto dist=[&](int a,int b) -> int {
		int res=N;
		int l=E[a],r=E[b];
		if(l>r) swap(l,r);
		r++;
		l+=2*N;
		r+=2*N;
		while(l<r){
			if(l&1) chmin(res,seg[l++]);
			if(r&1) chmin(res,seg[--r]);
			l>>=1;r>>=1;
		}
		return depth[a]+depth[b]-2*res;
	};

	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&&A[order[L]]==A[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){
			ans[order[i]]=max(dist(order[i],X),dist(order[i],Y));
		}
		L=R;
	}

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