結果

問題 No.2634 Tree Distance 3
ユーザー Yu_212Yu_212
提出日時 2024-02-18 14:17:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,583 ms / 3,000 ms
コード長 4,010 bytes
コンパイル時間 2,843 ms
コンパイル使用メモリ 265,816 KB
実行使用メモリ 335,000 KB
最終ジャッジ日時 2024-09-29 00:28:47
合計ジャッジ時間 79,170 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,371 ms
316,436 KB
testcase_01 AC 1,376 ms
316,512 KB
testcase_02 AC 1,394 ms
316,436 KB
testcase_03 AC 1,373 ms
316,544 KB
testcase_04 AC 1,355 ms
316,376 KB
testcase_05 AC 1,343 ms
316,456 KB
testcase_06 AC 1,387 ms
316,376 KB
testcase_07 AC 1,364 ms
316,516 KB
testcase_08 AC 1,187 ms
304,692 KB
testcase_09 AC 1,156 ms
305,096 KB
testcase_10 AC 1,174 ms
304,988 KB
testcase_11 AC 1,377 ms
304,080 KB
testcase_12 AC 1,388 ms
303,460 KB
testcase_13 AC 1,173 ms
305,316 KB
testcase_14 AC 1,387 ms
303,860 KB
testcase_15 AC 1,183 ms
304,964 KB
testcase_16 AC 1,148 ms
257,016 KB
testcase_17 AC 652 ms
152,380 KB
testcase_18 AC 850 ms
192,956 KB
testcase_19 AC 1,000 ms
225,244 KB
testcase_20 AC 274 ms
73,216 KB
testcase_21 AC 1,503 ms
320,592 KB
testcase_22 AC 1,481 ms
320,760 KB
testcase_23 AC 1,485 ms
320,656 KB
testcase_24 AC 1,507 ms
312,332 KB
testcase_25 AC 1,528 ms
311,212 KB
testcase_26 AC 1,578 ms
322,760 KB
testcase_27 AC 1,517 ms
312,436 KB
testcase_28 AC 1,525 ms
312,464 KB
testcase_29 AC 1,516 ms
320,276 KB
testcase_30 AC 1,533 ms
325,984 KB
testcase_31 AC 1,526 ms
325,712 KB
testcase_32 AC 1,549 ms
325,844 KB
testcase_33 AC 1,002 ms
224,280 KB
testcase_34 AC 190 ms
46,976 KB
testcase_35 AC 574 ms
132,736 KB
testcase_36 AC 295 ms
75,264 KB
testcase_37 AC 692 ms
159,388 KB
testcase_38 AC 7 ms
6,816 KB
testcase_39 AC 7 ms
6,820 KB
testcase_40 AC 5 ms
6,816 KB
testcase_41 AC 4 ms
6,816 KB
testcase_42 AC 4 ms
6,816 KB
testcase_43 AC 432 ms
100,096 KB
testcase_44 AC 268 ms
68,096 KB
testcase_45 AC 1,268 ms
274,676 KB
testcase_46 AC 812 ms
179,024 KB
testcase_47 AC 1,441 ms
307,696 KB
testcase_48 AC 1,510 ms
325,868 KB
testcase_49 AC 1,572 ms
325,740 KB
testcase_50 AC 1,583 ms
325,668 KB
testcase_51 AC 1,511 ms
325,920 KB
testcase_52 AC 1,498 ms
325,932 KB
testcase_53 AC 5 ms
6,820 KB
testcase_54 AC 5 ms
6,816 KB
testcase_55 AC 5 ms
6,816 KB
testcase_56 AC 4 ms
6,824 KB
testcase_57 AC 5 ms
6,816 KB
testcase_58 AC 1 ms
6,820 KB
testcase_59 AC 1 ms
6,820 KB
testcase_60 AC 1,441 ms
334,732 KB
testcase_61 AC 1,419 ms
335,000 KB
testcase_62 AC 1,421 ms
325,400 KB
testcase_63 AC 1,248 ms
290,996 KB
testcase_64 AC 758 ms
187,852 KB
testcase_65 AC 1,145 ms
273,996 KB
testcase_66 AC 292 ms
80,000 KB
testcase_67 AC 231 ms
66,816 KB
testcase_68 AC 1,319 ms
300,572 KB
testcase_69 AC 1,284 ms
300,548 KB
testcase_70 AC 1,300 ms
300,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using graph = vector<vector<int>>;
template <class F>
void one_third_centroid_decomposition(graph &g, F f) {
    int n = g.size();
    assert(n >= 1);
    vector<int> size(n), xy(n), count(n), sort(n);
    using buf_node = pair<graph, graph>;
    vector<unique_ptr<buf_node>> buf;
    auto get_buf = [&](int depth) -> buf_node& {
        while (depth >= buf.size())
            buf.push_back(make_unique<buf_node>(graph(n), graph(n)));
        return *buf[depth];
    };
    auto dc = [&](auto &dc, int n, int v, graph &g, int depth) -> void {
        if (n <= 2) return;
        int s = -1;
        auto sz = [&](auto &sz, int v, int p) -> void {
            size[v] = 1;
            int mx = 0;
            for (int e : g[v]) {
                if (e != p) {
                    sz(sz, e, v);
                    size[v] += size[e];
                    mx = max(mx, size[e]);
                }
            }
            mx = max(mx, n - size[v]);
            if (mx * 2 <= n)
                s = v;
        };
        sz(sz, v, -1);
        int maxsz = 0;
        for (int e : g[s]) {
            if (size[e] > size[s])
                size[e] = n - size[s];
            count[size[e]]++;
            maxsz = max(maxsz, size[e] + 1);
        }
        for (int i = 1; i < maxsz; i++)
            count[i] += count[i - 1];
        for (int e : g[s])
            sort[--count[size[e]]] = e;
        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();
        auto build = [&](auto &build, int v, int p, auto &t) -> void {
            t[v] = g[v];
            for (int e : g[v]) {
                if (e != p) build(build, e, v, t);
            }
        };
        for (int e : g[s]) {
            auto &t = xy[e] ? y : x;
            t[s].push_back(e);
            build(build, 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);
    auto f = [&](int s, auto &x, 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, auto &x, 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