結果

問題 No.2634 Tree Distance 3
ユーザー noya2noya2
提出日時 2024-02-16 16:55:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,849 bytes
コンパイル時間 3,204 ms
コンパイル使用メモリ 258,624 KB
実行使用メモリ 21,836 KB
最終ジャッジ日時 2024-02-16 18:07:07
合計ジャッジ時間 12,813 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
9,624 KB
testcase_01 AC 117 ms
9,624 KB
testcase_02 AC 117 ms
9,624 KB
testcase_03 AC 114 ms
9,624 KB
testcase_04 AC 113 ms
9,624 KB
testcase_05 AC 100 ms
9,624 KB
testcase_06 AC 111 ms
9,624 KB
testcase_07 AC 104 ms
9,624 KB
testcase_08 AC 80 ms
9,624 KB
testcase_09 AC 83 ms
9,624 KB
testcase_10 AC 83 ms
9,624 KB
testcase_11 AC 79 ms
9,624 KB
testcase_12 AC 78 ms
9,624 KB
testcase_13 AC 79 ms
9,624 KB
testcase_14 AC 69 ms
9,624 KB
testcase_15 AC 72 ms
9,624 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;
    }
}

template<class E>
struct csr {
    csr (int n_ = 0, int m_ = -1) : n(n_), m(m_) {
        if (m >= 0){
            es.reserve(m);
            start.reserve(m);
        }
        if (m == 0){
            build();
        }
    }
    int add(int idx, E elem){
        int eid = start.size();
        es.emplace_back(elem);
        start.emplace_back(idx);
        if (eid+1 == m) build();
        return eid;
    }
    void build(){
        if (m == -2) return ;
        m = start.size();
        std::vector<E> nes(m);
        std::vector<int> nstart(n+2,0);
        for (int i = 0; i < m; i++) nstart[start[i]+2]++;
        for (int i = 1; i < n; i++) nstart[i+2] += nstart[i+1];
        for (int i = 0; i < m; i++) nes[nstart[start[i]+1]++] = es[i];
        swap(es,nes);
        swap(start,nstart);
        m = -2;
    }
    const auto operator[](int idx) const {
        assert(m == -2);
        return std::ranges::subrange(es.begin()+start[idx],es.begin()+start[idx+1]);
    }
  private:
    int n, m;
    std::vector<E> es;
    std::vector<int> start;
};

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);
    csr<int> g(n,(n-1)*2);
    for (int i = 0; i < n-1; i++){
        int u, v; input(u), input(v); u--, v--;
        g.add(u,v);
        g.add(v,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