結果

問題 No.2634 Tree Distance 3
ユーザー noya2noya2
提出日時 2024-02-16 16:47:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,810 bytes
コンパイル時間 2,874 ms
コンパイル使用メモリ 256,944 KB
実行使用メモリ 28,728 KB
最終ジャッジ日時 2024-02-16 18:06:54
合計ジャッジ時間 12,786 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

template<typename T>
bool chmax(T &a, const T &b){
    if (a >= b) return false;
    a = b;
    return true;
}

void input(int &x){
    char c;
    x = 0;
    while ((c = getchar_unlocked() ^ 48) < 10){
        x *= 10;
        x += c;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; input(n);
    vector<int> a(n);
    for (auto &x : a) input(x);
    vector<vector<int>> g(n);
    for (int i = 0; i < n-1; i++){
        int u, v; input(u), input(v); u--, v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    vector<int> ids(n); iota(ids.begin(),ids.end(),0);
    sort(ids.begin(),ids.end(),[&](int l, int r){
        return a[l] > a[r];
    });
    vector<int> dmax(n,0);
    vector<int> from(n,-1);
    auto dfs_update = [&](auto sfs, int v, int f) -> void {
        for (int u : g[v]){
            if (u == f) continue;
            dmax[u] = dmax[v]+1;
            from[u] = v;
            sfs(sfs,u,v);
        }
    };
    dfs_update(dfs_update,ids[0],-1);
    auto dfs = [&](auto sfs, int v, int f, int d) -> void {
        int u = from[v];
        if (u == f) return ;
        assert(u != -1);
        if (chmax(dmax[u],d+1)){
            from[u] = v;
            dfs_update(dfs_update,u,v);
        }
        else {
            sfs(sfs,u,v,d+1);
        }
    };
    vector<int> ans(n);
    for (int l = 0, r = 0; l < n; l = r){
        while (r < n && a[ids[r]] == a[ids[l]]){
            if (r != 0){
                dfs(dfs,ids[r],-1,0);
            }
            r++;
        }
        for (int i = l; i < r; i++){
            ans[ids[i]] = dmax[ids[i]];
        }
    }
    for (int i = 0; i < n; i++){
        if (i != 0) cout << ' ';
        cout << ans[i];
    }
    cout << '\n';
}
0