結果

問題 No.2634 Tree Distance 3
ユーザー umimelumimel
提出日時 2024-02-16 23:01:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 881 ms / 3,000 ms
コード長 4,136 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 193,148 KB
実行使用メモリ 55,116 KB
最終ジャッジ日時 2024-09-28 21:36:59
合計ジャッジ時間 34,683 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 501 ms
47,428 KB
testcase_01 AC 501 ms
47,616 KB
testcase_02 AC 478 ms
47,488 KB
testcase_03 AC 470 ms
47,604 KB
testcase_04 AC 502 ms
47,592 KB
testcase_05 AC 458 ms
42,984 KB
testcase_06 AC 446 ms
43,048 KB
testcase_07 AC 464 ms
42,952 KB
testcase_08 AC 387 ms
45,552 KB
testcase_09 AC 400 ms
45,552 KB
testcase_10 AC 403 ms
45,540 KB
testcase_11 AC 391 ms
45,536 KB
testcase_12 AC 378 ms
45,612 KB
testcase_13 AC 326 ms
40,800 KB
testcase_14 AC 309 ms
40,544 KB
testcase_15 AC 311 ms
40,656 KB
testcase_16 AC 503 ms
49,744 KB
testcase_17 AC 286 ms
30,056 KB
testcase_18 AC 371 ms
36,036 KB
testcase_19 AC 418 ms
44,484 KB
testcase_20 AC 101 ms
17,664 KB
testcase_21 AC 743 ms
50,120 KB
testcase_22 AC 720 ms
53,524 KB
testcase_23 AC 665 ms
55,116 KB
testcase_24 AC 698 ms
50,348 KB
testcase_25 AC 720 ms
48,644 KB
testcase_26 AC 708 ms
51,164 KB
testcase_27 AC 881 ms
43,708 KB
testcase_28 AC 820 ms
45,116 KB
testcase_29 AC 754 ms
44,236 KB
testcase_30 AC 653 ms
41,944 KB
testcase_31 AC 635 ms
41,808 KB
testcase_32 AC 616 ms
41,864 KB
testcase_33 AC 359 ms
30,728 KB
testcase_34 AC 52 ms
8,960 KB
testcase_35 AC 165 ms
19,772 KB
testcase_36 AC 92 ms
12,800 KB
testcase_37 AC 215 ms
22,428 KB
testcase_38 AC 3 ms
6,820 KB
testcase_39 AC 4 ms
6,816 KB
testcase_40 AC 3 ms
6,816 KB
testcase_41 AC 3 ms
6,816 KB
testcase_42 AC 4 ms
6,820 KB
testcase_43 AC 141 ms
17,380 KB
testcase_44 AC 89 ms
12,672 KB
testcase_45 AC 497 ms
39,768 KB
testcase_46 AC 289 ms
27,472 KB
testcase_47 AC 587 ms
43,904 KB
testcase_48 AC 617 ms
46,268 KB
testcase_49 AC 635 ms
46,352 KB
testcase_50 AC 651 ms
46,420 KB
testcase_51 AC 667 ms
46,344 KB
testcase_52 AC 627 ms
46,340 KB
testcase_53 AC 3 ms
6,820 KB
testcase_54 AC 3 ms
6,820 KB
testcase_55 AC 3 ms
6,816 KB
testcase_56 AC 3 ms
6,820 KB
testcase_57 AC 3 ms
6,816 KB
testcase_58 AC 2 ms
6,820 KB
testcase_59 AC 2 ms
6,816 KB
testcase_60 AC 492 ms
46,060 KB
testcase_61 AC 506 ms
46,128 KB
testcase_62 AC 500 ms
46,148 KB
testcase_63 AC 353 ms
44,200 KB
testcase_64 AC 217 ms
30,292 KB
testcase_65 AC 335 ms
41,848 KB
testcase_66 AC 78 ms
15,120 KB
testcase_67 AC 61 ms
12,936 KB
testcase_68 AC 286 ms
40,376 KB
testcase_69 AC 303 ms
41,144 KB
testcase_70 AC 299 ms
40,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


template<typename T>
struct edge{
    int from, to;
    T cost;
    int id;

    edge(){}
    edge(int to, T cost=1) : from(-1), to(to), cost(cost){}
    edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){}
};

template<typename T>
struct redge{
    int from, to;
    T cap, cost;
    int rev;
    
    redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){}
    redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){}
};

template<typename T> using Edges = vector<edge<T>>;
template<typename T> using weighted_graph = vector<Edges<T>>;
template<typename T> using tree = vector<Edges<T>>;
using unweighted_graph = vector<vector<int>>;
template<typename T> using residual_graph = vector<vector<redge<T>>>;


template<typename W>
struct lowest_common_ancestor{
    int root = 0;
    int n;
    int log_n = 1;
    vector<vector<int>> par;
    vector<int> depth;

    lowest_common_ancestor(tree<W> &T, int root=0) : root(root){
        n = (int)T.size();
        while((1 << log_n) < n) log_n++;
        par.resize(log_n, vector<int>(n, -1));
        depth.resize(n, 0);
        init(T);
    }

    void init(tree<W> &T){
        function<void(int, int)> dfs = [&](int v, int p){
            par[0][v] = p;
            for(edge<W> e : T[v]) if(e.to!=p){
                depth[e.to] = depth[v]+1;
                dfs(e.to, v);
            }
        };

        dfs(root, -1);
        for(int k=0; k+1<log_n; k++){
            for(int v=0; v<n; v++){
                if(par[k][v]<0) par[k+1][v] = -1;
                else par[k+1][v] = par[k][par[k][v]];
            }
        }
    }

    int query(int u, int v){
        if(depth[u] < depth[v]) swap(u, v);
        for(int k=0; k<log_n; k++) if((depth[u]-depth[v]) >> k & 1) u = par[k][u];

        if(u == v) return u;
        for(int k=log_n-1; k>=0; k--){
            if(par[k][u] != par[k][v]){
                u = par[k][u];
                v = par[k][v];
            }
        }

        return par[0][u];
    }
};


void solve(){
	int n; cin >> n;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin >> a[i];
	{
		vector<int> c(n);
		for(int i=0; i<n; i++) c[i] = a[i];
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int i=0; i<n; i++) a[i] = lower_bound(all(c), a[i])-c.begin();
	}

	tree<int> T(n);
	for(int i=0; i<n-1; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		T[u].pb(edge<int>(v));
		T[v].pb(edge<int>(u));
	}

	lowest_common_ancestor<int> lca(T);
	vector<int> depth(n, 0);
	function<void(int, int)> dfs = [&](int v, int p){
		for(edge<int> e : T[v]) if(e.to!=p){
			depth[e.to] = depth[v]+1;
			dfs(e.to, v);
		}
	}; dfs(0, -1);

	function<int(int, int)> dist = [&](int u, int v){
		return depth[u] + depth[v] -2*depth[lca.query(u, v)];
	};

	vector<vector<int>> b(n);
	for(int i=0; i<n; i++) b[a[i]].push_back(i);

	int s = -1, t = -1;
	int diam = 0;
	vector<int> ans(n, 0);
	for(int i=n-1; i>=0; i--){
		//追加
		for(int v : b[i]){
			if(s==-1){
				s = v;
				t = v;
				diam = 0;
			}else{
				int s_dist = dist(s, v);
				int t_dist = dist(t, v);
				if(diam < max(s_dist, t_dist)){
					if(s_dist>=t_dist){
						t = v;
						diam = s_dist;
					}else{
						s = v;
						diam = t_dist;
					}
				}
			}
		}

		for(int v : b[i]){
			ans[v] = max(dist(s, v), dist(t, v));
		}
	}

	for(int i=0; i<n; i++) cout << ans[i]  << " ";
	cout << '\n';
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	
	int T=1;
	//cin >> T;
	while(T--) solve();
}
0