結果

問題 No.2677 Minmax Independent Set
ユーザー てんぷらてんぷら
提出日時 2024-03-15 22:47:34
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 4,329 bytes
コンパイル時間 5,915 ms
コンパイル使用メモリ 317,128 KB
実行使用メモリ 54,204 KB
最終ジャッジ日時 2024-03-15 22:47:47
合計ジャッジ時間 12,075 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 139 ms
17,980 KB
testcase_06 AC 136 ms
18,492 KB
testcase_07 AC 140 ms
18,876 KB
testcase_08 AC 150 ms
19,900 KB
testcase_09 AC 143 ms
21,820 KB
testcase_10 AC 81 ms
15,132 KB
testcase_11 AC 81 ms
15,132 KB
testcase_12 AC 107 ms
54,204 KB
testcase_13 AC 127 ms
28,092 KB
testcase_14 AC 118 ms
28,092 KB
testcase_15 AC 137 ms
36,412 KB
testcase_16 AC 113 ms
15,132 KB
testcase_17 AC 143 ms
15,132 KB
testcase_18 AC 62 ms
10,880 KB
testcase_19 AC 76 ms
12,188 KB
testcase_20 AC 22 ms
6,548 KB
testcase_21 AC 88 ms
13,212 KB
testcase_22 AC 7 ms
6,548 KB
testcase_23 AC 2 ms
6,548 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 74 ms
15,132 KB
testcase_29 AC 171 ms
39,224 KB
testcase_30 AC 2 ms
6,548 KB
testcase_31 AC 2 ms
6,548 KB
testcase_32 AC 2 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
testcase_36 AC 2 ms
6,548 KB
testcase_37 AC 2 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 3 ms
6,548 KB
testcase_40 AC 3 ms
6,548 KB
testcase_41 AC 4 ms
6,548 KB
testcase_42 AC 6 ms
6,548 KB
testcase_43 AC 11 ms
6,548 KB
testcase_44 AC 22 ms
6,548 KB
testcase_45 AC 48 ms
9,088 KB
testcase_46 AC 126 ms
14,844 KB
testcase_47 AC 128 ms
15,132 KB
testcase_48 AC 131 ms
15,132 KB
testcase_49 AC 122 ms
15,132 KB
testcase_50 AC 27 ms
7,808 KB
testcase_51 AC 4 ms
6,548 KB
testcase_52 AC 62 ms
13,092 KB
testcase_53 AC 59 ms
12,364 KB
testcase_54 AC 3 ms
6,548 KB
testcase_55 AC 80 ms
15,132 KB
testcase_56 AC 83 ms
15,132 KB
testcase_57 AC 77 ms
15,132 KB
testcase_58 AC 82 ms
15,132 KB
testcase_59 AC 83 ms
15,132 KB
testcase_60 AC 62 ms
15,132 KB
testcase_61 AC 67 ms
15,132 KB
testcase_62 AC 72 ms
22,204 KB
testcase_63 AC 83 ms
34,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
using namespace atcoder;
using mint = modint998244353;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

template <class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)>
struct RerootingDP {
    struct edge {
        int to, idx, xdi;
    };
    RerootingDP(int n_ = 0) : n(n_), inner_edge_id(0) {
        es.resize(2 * n - 2);
        start.resize(2 * n - 2);
        if(n == 1)
            es_build();
    }
    void add_edge(int u, int v, int idx, int xdi) {
        start[inner_edge_id] = u;
        es[inner_edge_id] = {v, idx, xdi};
        inner_edge_id++;
        start[inner_edge_id] = v;
        es[inner_edge_id] = {u, xdi, idx};
        inner_edge_id++;
        if(inner_edge_id == 2 * n - 2) {
            es_build();
        }
    }
    vector<V> build(int root_ = 0) {
        root = root_;
        vector<V> subdp(n);
        subdp[0] = put_vertex(e(), 0);
        outs.resize(n);
        vector<int> geta(n + 1, 0);
        for(int i = 0; i < n; i++)
            geta[i + 1] = start[i + 1] - start[i] - 1;
        geta[root + 1]++;
        for(int i = 0; i < n; i++)
            geta[i + 1] += geta[i];
        auto dfs = [&](auto sfs, int v, int f) -> void {
            E val = e();
            for(int i = start[v]; i < start[v + 1]; i++) {
                if(es[i].to == f) {
                    swap(es[start[v + 1] - 1], es[i]);
                }
                if(es[i].to == f)
                    continue;
                sfs(sfs, es[i].to, v);
                E nval = put_edge(subdp[es[i].to], es[i].idx);
                outs[geta[v]++] = nval;
                val = merge(val, nval);
            }
            subdp[v] = put_vertex(val, v);
        };
        dfs(dfs, root, -1);
        return subdp;
    }
    vector<V> reroot() {
        vector<E> reverse_edge(n);
        reverse_edge[root] = e();
        vector<V> answers(n);
        auto dfs = [&](auto sfs, int v) -> void {
            int le = outs_start(v);
            int ri = outs_start(v + 1);
            int siz = ri - le;
            vector<E> rui(siz + 1);
            rui[siz] = e();
            for(int i = siz - 1; i >= 0; i--) {
                rui[i] = merge(outs[le + i], rui[i + 1]);
            }
            answers[v] = put_vertex(merge(rui[0], reverse_edge[v]), v);
            E lui = e();
            for(int i = 0; i < siz; i++) {
                V rdp = put_vertex(merge(merge(lui, rui[i + 1]), reverse_edge[v]), v);
                reverse_edge[es[start[v] + i].to] = put_edge(rdp, es[start[v] + i].xdi);
                lui = merge(lui, outs[le + i]);
                sfs(sfs, es[start[v] + i].to);
            }
        };
        dfs(dfs, root);
        return answers;
    }

  private:
    int n, root, inner_edge_id;
    vector<E> outs;
    vector<edge> es;
    vector<int> start;
    int outs_start(int v) {
        int res = start[v] - v;
        if(root < v)
            res++;
        return res;
    }
    void es_build() {
        vector<edge> nes(2 * n - 2);
        vector<int> nstart(n + 2, 0);
        for(int i = 0; i < 2 * n - 2; i++)
            nstart[start[i] + 2]++;
        for(int i = 0; i < n; i++)
            nstart[i + 1] += nstart[i];
        for(int i = 0; i < 2 * n - 2; i++)
            nes[nstart[start[i] + 1]++] = es[i];
        swap(es, nes);
        swap(start, nstart);
    }
};

using P = pair<int, int>;

P e() {
    return {0, 0};
}

P merge(P x, P y) {
    return {x.first + y.first, x.second + y.second};
}

P put_edge(P x, int i) {
    return {max(x.second, x.first), x.first};
}

P put_vertex(P x, int i) {
    return {x.first, x.second + 1};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    RerootingDP<P, P, merge, e, put_edge, put_vertex> g(n);
    for(int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g.add_edge(u - 1, v - 1, i, i);
    }
    g.build();
    int ans = inf;
    for(auto ret : g.reroot()) {
        ans = min(ans, ret.second);
    }
    cout << ans << endl;
    return 0;
}
0