結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 eoeoeoeo
提出日時 2024-03-13 19:59:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 677 ms / 2,000 ms
コード長 4,465 bytes
コンパイル時間 2,467 ms
コンパイル使用メモリ 221,016 KB
実行使用メモリ 122,148 KB
最終ジャッジ日時 2024-03-15 20:53:48
合計ジャッジ時間 24,499 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 606 ms
107,684 KB
testcase_06 AC 604 ms
107,940 KB
testcase_07 AC 605 ms
108,196 KB
testcase_08 AC 615 ms
108,580 KB
testcase_09 AC 612 ms
109,476 KB
testcase_10 AC 549 ms
112,404 KB
testcase_11 AC 540 ms
112,404 KB
testcase_12 AC 501 ms
122,148 KB
testcase_13 AC 634 ms
114,580 KB
testcase_14 AC 614 ms
115,696 KB
testcase_15 AC 654 ms
118,140 KB
testcase_16 AC 629 ms
105,636 KB
testcase_17 AC 637 ms
105,636 KB
testcase_18 AC 415 ms
68,672 KB
testcase_19 AC 473 ms
80,508 KB
testcase_20 AC 158 ms
30,592 KB
testcase_21 AC 548 ms
88,852 KB
testcase_22 AC 36 ms
11,008 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 544 ms
113,124 KB
testcase_29 AC 642 ms
115,048 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 8 ms
6,676 KB
testcase_41 AC 13 ms
6,676 KB
testcase_42 AC 28 ms
9,600 KB
testcase_43 AC 64 ms
15,872 KB
testcase_44 AC 147 ms
28,288 KB
testcase_45 AC 312 ms
53,112 KB
testcase_46 AC 626 ms
102,920 KB
testcase_47 AC 620 ms
104,484 KB
testcase_48 AC 641 ms
104,484 KB
testcase_49 AC 668 ms
104,612 KB
testcase_50 AC 202 ms
44,572 KB
testcase_51 AC 13 ms
6,784 KB
testcase_52 AC 464 ms
93,432 KB
testcase_53 AC 414 ms
86,472 KB
testcase_54 AC 11 ms
6,676 KB
testcase_55 AC 658 ms
113,140 KB
testcase_56 AC 661 ms
113,068 KB
testcase_57 AC 668 ms
113,084 KB
testcase_58 AC 677 ms
113,136 KB
testcase_59 AC 660 ms
113,096 KB
testcase_60 AC 343 ms
104,868 KB
testcase_61 AC 335 ms
105,500 KB
testcase_62 AC 341 ms
109,604 KB
testcase_63 AC 337 ms
115,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// nu50218's library
// 以下の形で計算する木DPを、すべての根について計算する。
// dp[r] = f(r, op{ g(r, c, dp[c]) | c in children[r]})
// ただし、以下を満たす。
// - op は演算の順序に寄らない
// - e() は op の単位元
// - op(\emptyset) := e()
// - op(x) := x
template <class S, S (*op)(S, S), S (*e)(), S (*f)(int, S), S (*g)(int, int, S)>
struct ReRooting {
    ReRooting(int _n) : n(_n), adj(_n), dp(_n), gdp(_n), idx(_n), product_left(_n), product_right(_n) {}

    void add_edge(int u, int v) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    void calc() {
        calculated = true;
        if (n == 1) return;

        for (int i = 0; i < n; i++) {
            int cnt = 0;
            for (auto&& x : adj[i]) {
                idx[i][x] = cnt;
                cnt++;
            }
            dp[i].resize(cnt);
            gdp[i].resize(cnt);
        }

        rec(0);
        propagate(0);
    }

    S value(int r, int par = -1) {
        assert(calculated);
        assert(0 <= r && r < n);
        assert(par < n);

        if (n == 1) return f(r, e());

        if (idx[r].count(par) == 0) {
            return f(r, product_right[r].front());
        }

        const int i = idx[r][par];
        const int deg = adj[r].size();

        S prod_l = (i != 0 ? product_left[r][i - 1] : e());
        S prod_r = (i != deg - 1 ? product_right[r][i + 1] : e());

        return f(r, op(prod_l, prod_r));
    }

   private:
    int n;
    std::vector<std::vector<int>> adj;
    bool calculated = false;

    // dp[i][idx[j]] := iを削除して得られる連結成分のうちjを根とした木に対する木DPの結果
    std::vector<std::vector<S>> dp;
    // gdp[i][idx[j]] := g(i, j, dp[i][idx[j]])
    std::vector<std::vector<S>> gdp;
    std::vector<std::unordered_map<int, int>> idx;

    // product_{left/right} := (左/右)からのdp[i]の総積
    std::vector<std::vector<S>> product_left;
    std::vector<std::vector<S>> product_right;

    S rec(int r, int par = -1) {
        S prod = e();

        for (auto&& c : adj[r]) {
            if (c == par) continue;
            S val = rec(c, r);
            prod = op(prod, g(r, c, val));
        }

        S ret = f(r, prod);
        if (par != -1) {
            dp[par][idx[par][r]] = ret;
            gdp[par][idx[par][r]] = g(par, r, ret);
        }
        return ret;
    }

    void propagate(int r, int par = -1, S par_val = e()) {
        // fill dp[root]
        if (par != -1) {
            dp[r][idx[r][par]] = par_val;
            gdp[r][idx[r][par]] = g(r, par, par_val);
        }

        // construct product_{left/right}[root] from gdp[root]
        const int deg = adj[r].size();
        product_left[r].resize(deg);
        product_left[r][0] = gdp[r][0];
        product_right[r].resize(deg);
        product_right[r][deg - 1] = gdp[r][deg - 1];
        for (int i = 1; i < deg; i++) {
            product_left[r][i] = op(gdp[r][i], product_left[r][i - 1]);
        }
        for (int i = deg - 2; i >= 0; i--) {
            product_right[r][i] = op(gdp[r][i], product_right[r][i + 1]);
        }

        // propagate
        for (auto&& c : adj[r]) {
            if (c == par) continue;
            propagate(c, r, value(r, c));
        }
    }
};

// dp[r][0] := r を黒く塗らなかったときの最大独立集合のサイズ
// dp[r][1] := r を黒く塗ったときの最大独立集合のサイズ

// 遷移は以下のとおり
// dp[r][0] = \sum_{c \in children[r]} max(dp[c][0], dp[c][1])
// dp[r][1] = 1 + \sum_{c \in children[r]} dp[c][0]

using S = pair<int, int>;

S op(S x, S y) {
    return {x.first + y.first, x.second + y.second};
}

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

S f([[maybe_unused]] int r, S x) {
    return {x.first, 1 + x.second};
}

S g([[maybe_unused]] int r, [[maybe_unused]] int c, S x) {
    return {max(x.first, x.second), x.first};
}

int main() {
    int N;
    cin >> N;

    ReRooting<S, op, e, f, g> rerooting(N);

    for (int i = 0; i < N - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        rerooting.add_edge(u, v);
    }

    rerooting.calc();

    int ans = numeric_limits<int>::max();
    for (int i = 0; i < N; i++) {
        ans = min(ans, rerooting.value(i).second);
    }

    cout << ans << endl;
}
0