結果

問題 No.2634 Tree Distance 3
ユーザー 👑 potato167potato167
提出日時 2024-02-14 14:13:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,127 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 213,960 KB
実行使用メモリ 33,764 KB
最終ジャッジ日時 2024-02-16 18:05:22
合計ジャッジ時間 28,678 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 WA -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
権限があれば一括ダウンロードができます

ソースコード

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,len=(1<<12);
	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,B-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((l^r)/B==0){
			rep(i,l,r) chmin(res,base[i]);
		}
		else{
			chmin(res,TL[l]);
			chmin(res,TR[r]);
			l=(l+1)/B;
			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