結果

問題 No.2634 Tree Distance 3
ユーザー Yu_212Yu_212
提出日時 2024-02-16 23:26:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,758 bytes
コンパイル時間 3,678 ms
コンパイル使用メモリ 266,028 KB
実行使用メモリ 334,972 KB
最終ジャッジ日時 2024-02-16 23:29:01
合計ジャッジ時間 118,531 ms
ジャッジサーバーID
(参考情報)
judge16 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define var auto

template <class Edge> using graph = std::vector<std::vector<Edge>>;


template <class Edge> struct helper {
    static int to(const Edge &e) { return e.to; }
};
template <class T> struct helper<std::pair<int, T>> {
    static int to(const std::pair<int, T> &e) { return e.first; }
};
template <> struct helper<int> {
    static int to(const int &e) { return e; }
};
template <class Edge> int to(const Edge &e) { return helper<Edge>::to(e); }

// f(divide, L, R)
template <class Edge, class F>
void one_third_centroid_decomposition(const graph<Edge> &g, F f) {
    const int n = g.size();
    assert(n >= 1);
    std::vector<int> size(n), xy(n), count(n), sort(n);
    using buf_node = std::pair<graph<Edge>, graph<Edge>>;
    std::vector<std::unique_ptr<buf_node>> buf;
    const auto get_buf = [&](const int depth) -> buf_node & {
        while (depth >= buf.size())
            buf.push_back(std::make_unique<buf_node>(graph<Edge>(n), graph<Edge>(n)));
        return *buf[depth];
    };
    const auto dc = [&](const auto &dc, const int n, const int v,
                        const graph<Edge> &g, const int depth) -> void {
        if (n <= 2)
            return;
        int s = -1;
        const auto sz = [&](const auto &sz, const int v, const int p) -> void {
            size[v] = 1;
            int max = 0;
            for (const auto &e : g[v]) {
                if (to(e) != p) {
                    sz(sz, to(e), v);
                    size[v] += size[to(e)];
                    max = std::max(max, size[to(e)]);
                }
            }
            max = std::max(max, n - size[v]);
            if (max * 2 <= n)
                s = v;
        };
        sz(sz, v, -1);
        int maxsz = 0;
        for (const auto &e : g[s]) {
            if (size[to(e)] > size[s])
                size[to(e)] = n - size[s];
            count[size[to(e)]]++;
            maxsz = std::max(maxsz, size[to(e)] + 1);
        }
        for (int i = 1; i < maxsz; i++)
            count[i] += count[i - 1];
        for (const auto &e : g[s])
            sort[--count[size[to(e)]]] = to(e);
        std::fill(count.begin(), count.begin() + maxsz, 0);
        int xs = 0, ys = 0;
        for (int i = g[s].size(); i-- > 0;) {
            if (xs < ys)
                xy[sort[i]] = 0, xs += size[sort[i]];
            else
                xy[sort[i]] = 1, ys += size[sort[i]];
        }
        auto &[x, y] = get_buf(depth);
        x[s].clear();
        y[s].clear();
        const auto build = [&](const auto &build, const int v, const int p, auto &t) -> void {
            t[v] = g[v];
            for (const auto &e : g[v]) {
                if (to(e) != p) build(build, to(e), v, t);
            }
        };
        for (const auto &e : g[s]) {
            auto &t = xy[to(e)] ? y : x;
            t[s].push_back(e);
            build(build, to(e), s, t);
        }
        f(s, x, y);
        dc(dc, xs + 1, s, x, depth + 1);
        dc(dc, ys + 1, s, y, depth + 1);
    };
    dc(dc, n, 0, g, 0);
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<int>> edges(n);
    for (int i = 0; i < n - 1; i ++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    vector<int> ans(n);
    const auto f = [&](const int s, const auto &x, const auto &y) {
        vector<int> table;
        auto dfs1 = [&](auto self, int node, int parent, int depth) -> void {
            while (table.size() <= depth) table.push_back(0);
            table[depth] = min(table[depth], -a[node]);
            for (int child : x[node]) {
                if (child == parent) continue;
                self(self, child, node, depth + 1);
            }
        };
        dfs1(dfs1, s, s, 0);
        int m = table.size();
        for (int i = m - 2; i >= 0; i--) {
            table[i] = min(table[i], table[i + 1]);
        }
        auto dfs2 = [&](auto self, int node, int parent, int depth) -> void {
            int pos = upper_bound(table.begin(), table.end(), -a[node]) - table.begin();
            ans[node] = max(ans[node], depth + pos - 1);
            for (int child : y[node]) {
                if (child == parent) continue;
                self(self, child, node, depth + 1);
            }
        };
        dfs2(dfs2, s, s, 0);
    };
    one_third_centroid_decomposition(edges, [&](int s, const auto &x, const auto &y) {
        f(s, x, y);
        f(s, y, x);
    });
    for (int i = 0; i < n; i++) {
        cout << ans[i] << endl;
    }
}
0