結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-11 17:34:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 637 ms / 3,000 ms
コード長 2,039 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 216,272 KB
実行使用メモリ 61,792 KB
最終ジャッジ日時 2024-02-16 18:03:03
合計ジャッジ時間 37,435 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 608 ms
51,936 KB
testcase_01 AC 633 ms
51,936 KB
testcase_02 AC 580 ms
51,936 KB
testcase_03 AC 589 ms
51,936 KB
testcase_04 AC 592 ms
51,936 KB
testcase_05 AC 592 ms
51,936 KB
testcase_06 AC 581 ms
51,936 KB
testcase_07 AC 574 ms
51,936 KB
testcase_08 AC 545 ms
52,836 KB
testcase_09 AC 553 ms
52,884 KB
testcase_10 AC 576 ms
52,864 KB
testcase_11 AC 571 ms
52,796 KB
testcase_12 AC 580 ms
52,772 KB
testcase_13 AC 547 ms
52,904 KB
testcase_14 AC 557 ms
52,780 KB
testcase_15 AC 542 ms
52,860 KB
testcase_16 AC 442 ms
50,824 KB
testcase_17 AC 253 ms
30,904 KB
testcase_18 AC 327 ms
37,248 KB
testcase_19 AC 392 ms
45,348 KB
testcase_20 AC 118 ms
17,532 KB
testcase_21 AC 548 ms
58,464 KB
testcase_22 AC 520 ms
60,768 KB
testcase_23 AC 522 ms
61,792 KB
testcase_24 AC 582 ms
55,264 KB
testcase_25 AC 579 ms
53,984 KB
testcase_26 AC 583 ms
55,648 KB
testcase_27 AC 555 ms
53,984 KB
testcase_28 AC 540 ms
55,136 KB
testcase_29 AC 558 ms
54,496 KB
testcase_30 AC 577 ms
52,124 KB
testcase_31 AC 578 ms
52,124 KB
testcase_32 AC 576 ms
52,124 KB
testcase_33 AC 359 ms
38,952 KB
testcase_34 AC 68 ms
10,484 KB
testcase_35 AC 203 ms
24,532 KB
testcase_36 AC 116 ms
15,292 KB
testcase_37 AC 245 ms
27,744 KB
testcase_38 AC 5 ms
6,676 KB
testcase_39 AC 5 ms
6,676 KB
testcase_40 AC 4 ms
6,676 KB
testcase_41 AC 4 ms
6,676 KB
testcase_42 AC 4 ms
6,676 KB
testcase_43 AC 162 ms
19,020 KB
testcase_44 AC 107 ms
13,616 KB
testcase_45 AC 500 ms
44,752 KB
testcase_46 AC 318 ms
30,268 KB
testcase_47 AC 573 ms
49,584 KB
testcase_48 AC 614 ms
52,124 KB
testcase_49 AC 601 ms
52,124 KB
testcase_50 AC 615 ms
52,124 KB
testcase_51 AC 636 ms
51,996 KB
testcase_52 AC 637 ms
52,124 KB
testcase_53 AC 4 ms
6,676 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 4 ms
6,676 KB
testcase_56 AC 4 ms
6,676 KB
testcase_57 AC 4 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 619 ms
51,936 KB
testcase_61 AC 634 ms
51,936 KB
testcase_62 AC 628 ms
51,936 KB
testcase_63 AC 596 ms
51,260 KB
testcase_64 AC 368 ms
34,380 KB
testcase_65 AC 521 ms
48,520 KB
testcase_66 AC 137 ms
16,744 KB
testcase_67 AC 113 ms
14,436 KB
testcase_68 AC 580 ms
52,724 KB
testcase_69 AC 550 ms
52,724 KB
testcase_70 AC 576 ms
52,724 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<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)]);
	};
	// p に含まれるやつから q に含まれるやつの距離を考える
	// q のやつを更新
	auto f=[&](vector<int> p,vector<int> q) -> void {
		vector<int> X={p[0],p[0],p[0]};
		rep(i,0,2) for(auto x:p) if(dist(X[i],X[i+1])<dist(X[i],x)) X[i+1]=x;
		for(auto x:q) rep(i,1,3) chmax(ans[x],dist(X[i],x));
	};
	vector<int> order(N);
	rep(i,0,N) order[i]=i;
	sort(all(order),[&](int l,int r){
		return A[l]<A[r];
	});
	vector<int> tmp={order[0]};
	rep(i,0,N){
		if(i==N-1||A[order[i]]!=A[order[i+1]]){
			f(tmp,tmp);
			if(i!=N-1) tmp={order[i+1]};
		}
		else tmp.push_back(order[i+1]);
	}
	auto g=[&](auto self,vector<int> p) -> void {
		int len=p.size();
		if(len<=1) return;
		vector<int> q1,q2;
		rep(i,0,p.size()){
			(i*2<len?q1:q2).push_back(p[i]);
		}
		f(q2,q1);
		self(self,q1);
		self(self,q2);
	};g(g,order);
	rep(i,0,N){
		if(i) cout<<" ";
		cout<<ans[i];
	}
	cout<<"\n";
}
0