結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-14 14:26:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 3,000 ms
コード長 2,126 bytes
コンパイル時間 2,468 ms
コンパイル使用メモリ 213,844 KB
実行使用メモリ 35,556 KB
最終ジャッジ日時 2024-02-16 18:05:50
合計ジャッジ時間 17,781 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 218 ms
25,828 KB
testcase_01 AC 205 ms
25,828 KB
testcase_02 AC 202 ms
25,828 KB
testcase_03 AC 210 ms
25,828 KB
testcase_04 AC 220 ms
25,828 KB
testcase_05 AC 202 ms
25,828 KB
testcase_06 AC 208 ms
25,828 KB
testcase_07 AC 192 ms
25,828 KB
testcase_08 AC 166 ms
26,724 KB
testcase_09 AC 166 ms
26,724 KB
testcase_10 AC 167 ms
26,724 KB
testcase_11 AC 181 ms
26,724 KB
testcase_12 AC 178 ms
26,596 KB
testcase_13 AC 169 ms
26,724 KB
testcase_14 AC 165 ms
26,596 KB
testcase_15 AC 152 ms
26,724 KB
testcase_16 AC 150 ms
30,116 KB
testcase_17 AC 89 ms
19,836 KB
testcase_18 AC 113 ms
22,784 KB
testcase_19 AC 133 ms
27,440 KB
testcase_20 AC 43 ms
13,440 KB
testcase_21 AC 189 ms
32,228 KB
testcase_22 AC 200 ms
34,532 KB
testcase_23 AC 190 ms
35,556 KB
testcase_24 AC 204 ms
29,156 KB
testcase_25 AC 203 ms
27,876 KB
testcase_26 AC 204 ms
29,540 KB
testcase_27 AC 192 ms
27,876 KB
testcase_28 AC 191 ms
28,900 KB
testcase_29 AC 193 ms
28,260 KB
testcase_30 AC 192 ms
25,956 KB
testcase_31 AC 194 ms
25,956 KB
testcase_32 AC 188 ms
25,956 KB
testcase_33 AC 124 ms
20,068 KB
testcase_34 AC 25 ms
8,704 KB
testcase_35 AC 71 ms
14,464 KB
testcase_36 AC 40 ms
10,752 KB
testcase_37 AC 80 ms
15,872 KB
testcase_38 AC 5 ms
6,676 KB
testcase_39 AC 5 ms
6,676 KB
testcase_40 AC 5 ms
6,676 KB
testcase_41 AC 5 ms
6,676 KB
testcase_42 AC 5 ms
6,676 KB
testcase_43 AC 57 ms
12,284 KB
testcase_44 AC 37 ms
10,112 KB
testcase_45 AC 170 ms
22,816 KB
testcase_46 AC 110 ms
17,152 KB
testcase_47 AC 196 ms
24,844 KB
testcase_48 AC 212 ms
25,956 KB
testcase_49 AC 219 ms
25,956 KB
testcase_50 AC 213 ms
25,956 KB
testcase_51 AC 210 ms
25,956 KB
testcase_52 AC 216 ms
25,956 KB
testcase_53 AC 5 ms
6,676 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 5 ms
6,676 KB
testcase_56 AC 5 ms
6,676 KB
testcase_57 AC 5 ms
6,676 KB
testcase_58 AC 4 ms
6,676 KB
testcase_59 AC 4 ms
6,676 KB
testcase_60 AC 221 ms
25,828 KB
testcase_61 AC 217 ms
25,828 KB
testcase_62 AC 218 ms
25,828 KB
testcase_63 AC 168 ms
26,012 KB
testcase_64 AC 107 ms
19,280 KB
testcase_65 AC 154 ms
24,772 KB
testcase_66 AC 44 ms
11,648 KB
testcase_67 AC 37 ms
10,624 KB
testcase_68 AC 166 ms
26,692 KB
testcase_69 AC 167 ms
26,692 KB
testcase_70 AC 165 ms
26,692 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);

	const int B=16,C=15,len=(1<<C);
	vector<int> base(N*2,N);
	vector table(B,vector<int>(len,N));
	vector<int> TL(N*2,N),TR(N*2+1,N);
	rep(i,0,N){
		base[E[i]]=depth[i];
		base[E[i+N]]=depth[i]-1;
	}
	rep(i,0,N*2){
		chmin(table[0][i/B],base[i]);
		TL[i]=base[i];
		if((i+1)%B==0){
			for(int j=i;j>i+1-B;j--){
				chmin(TL[j-1],TL[j]);
			}
		}
		else{
			chmin(TR[i+1],min(TR[i],base[i]));
		}
	}
	rep(i,0,C-1) rep(j,0,len){
		table[i+1][j]=table[i][j];
		if(j+(1<<i)<len) 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 res=N;
		if(r-l<B*2){
			rep(i,l,r) chmin(res,base[i]);
		}
		else{
			chmin(res,TL[l]);
			chmin(res,TR[r]);
			l=l/B+1;
			r=r/B;
			int z=31-__builtin_clz(r-l);
			if(z>=0) chmin(res,min(table[z][l],table[z][r-(1<<z)]));
		}
		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