結果

問題 No.2634 Tree Distance 3
ユーザー Yu_212Yu_212
提出日時 2024-02-16 23:35:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,817 ms / 3,000 ms
コード長 4,813 bytes
コンパイル時間 3,372 ms
コンパイル使用メモリ 265,840 KB
実行使用メモリ 335,072 KB
最終ジャッジ日時 2024-09-28 22:38:15
合計ジャッジ時間 92,674 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,539 ms
316,364 KB
testcase_01 AC 1,547 ms
316,360 KB
testcase_02 AC 1,487 ms
316,528 KB
testcase_03 AC 1,519 ms
316,516 KB
testcase_04 AC 1,547 ms
316,476 KB
testcase_05 AC 1,594 ms
316,512 KB
testcase_06 AC 1,537 ms
316,368 KB
testcase_07 AC 1,529 ms
316,452 KB
testcase_08 AC 1,374 ms
304,648 KB
testcase_09 AC 1,328 ms
305,144 KB
testcase_10 AC 1,334 ms
305,128 KB
testcase_11 AC 1,660 ms
304,112 KB
testcase_12 AC 1,570 ms
303,548 KB
testcase_13 AC 1,332 ms
305,308 KB
testcase_14 AC 1,612 ms
303,752 KB
testcase_15 AC 1,333 ms
305,128 KB
testcase_16 AC 1,313 ms
257,016 KB
testcase_17 AC 729 ms
152,212 KB
testcase_18 AC 941 ms
192,968 KB
testcase_19 AC 1,132 ms
225,212 KB
testcase_20 AC 312 ms
73,344 KB
testcase_21 AC 1,687 ms
320,680 KB
testcase_22 AC 1,756 ms
320,588 KB
testcase_23 AC 1,686 ms
320,652 KB
testcase_24 AC 1,737 ms
312,508 KB
testcase_25 AC 1,664 ms
311,308 KB
testcase_26 AC 1,728 ms
322,632 KB
testcase_27 AC 1,723 ms
312,536 KB
testcase_28 AC 1,700 ms
312,364 KB
testcase_29 AC 1,740 ms
320,340 KB
testcase_30 AC 1,682 ms
325,804 KB
testcase_31 AC 1,723 ms
325,836 KB
testcase_32 AC 1,722 ms
325,636 KB
testcase_33 AC 1,118 ms
224,304 KB
testcase_34 AC 205 ms
46,976 KB
testcase_35 AC 644 ms
132,608 KB
testcase_36 AC 319 ms
75,264 KB
testcase_37 AC 752 ms
159,340 KB
testcase_38 AC 7 ms
6,816 KB
testcase_39 AC 8 ms
6,816 KB
testcase_40 AC 5 ms
6,816 KB
testcase_41 AC 5 ms
6,820 KB
testcase_42 AC 5 ms
6,816 KB
testcase_43 AC 479 ms
100,224 KB
testcase_44 AC 298 ms
68,224 KB
testcase_45 AC 1,475 ms
274,772 KB
testcase_46 AC 908 ms
178,976 KB
testcase_47 AC 1,688 ms
307,680 KB
testcase_48 AC 1,794 ms
325,804 KB
testcase_49 AC 1,804 ms
325,672 KB
testcase_50 AC 1,774 ms
325,676 KB
testcase_51 AC 1,796 ms
325,980 KB
testcase_52 AC 1,792 ms
325,908 KB
testcase_53 AC 6 ms
6,816 KB
testcase_54 AC 5 ms
6,820 KB
testcase_55 AC 5 ms
6,816 KB
testcase_56 AC 5 ms
6,816 KB
testcase_57 AC 5 ms
6,816 KB
testcase_58 AC 1 ms
6,816 KB
testcase_59 AC 2 ms
6,816 KB
testcase_60 AC 1,736 ms
334,672 KB
testcase_61 AC 1,807 ms
335,072 KB
testcase_62 AC 1,817 ms
325,312 KB
testcase_63 AC 1,616 ms
291,184 KB
testcase_64 AC 982 ms
187,700 KB
testcase_65 AC 1,478 ms
274,056 KB
testcase_66 AC 350 ms
80,128 KB
testcase_67 AC 279 ms
66,816 KB
testcase_68 AC 1,600 ms
300,528 KB
testcase_69 AC 1,478 ms
300,648 KB
testcase_70 AC 1,541 ms
300,436 KB
権限があれば一括ダウンロードができます

ソースコード

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();
            if (pos >= 1) {
                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] << " \n"[i+1==n];
    }
}
0