結果

問題 No.2677 Minmax Independent Set
ユーザー NokonoKotlinNokonoKotlin
提出日時 2024-03-13 21:03:22
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 2,825 ms
コンパイル使用メモリ 254,704 KB
実行使用メモリ 46,288 KB
最終ジャッジ日時 2024-03-15 20:54:40
合計ジャッジ時間 12,320 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 224 ms
26,888 KB
testcase_06 AC 222 ms
27,116 KB
testcase_07 AC 227 ms
27,308 KB
testcase_08 AC 225 ms
27,880 KB
testcase_09 AC 225 ms
28,620 KB
testcase_10 AC 163 ms
24,236 KB
testcase_11 AC 166 ms
24,240 KB
testcase_12 AC 258 ms
46,288 KB
testcase_13 AC 241 ms
31,512 KB
testcase_14 AC 226 ms
31,628 KB
testcase_15 AC 254 ms
35,924 KB
testcase_16 AC 257 ms
24,824 KB
testcase_17 AC 256 ms
24,824 KB
testcase_18 AC 143 ms
17,016 KB
testcase_19 AC 181 ms
19,452 KB
testcase_20 AC 55 ms
9,088 KB
testcase_21 AC 204 ms
21,204 KB
testcase_22 AC 14 ms
6,676 KB
testcase_23 AC 2 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 2 ms
6,676 KB
testcase_28 AC 147 ms
23,020 KB
testcase_29 AC 239 ms
39,440 KB
testcase_30 AC 1 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 3 ms
6,676 KB
testcase_40 AC 4 ms
6,676 KB
testcase_41 AC 6 ms
6,676 KB
testcase_42 AC 12 ms
6,676 KB
testcase_43 AC 25 ms
6,676 KB
testcase_44 AC 50 ms
8,832 KB
testcase_45 AC 117 ms
14,268 KB
testcase_46 AC 252 ms
25,092 KB
testcase_47 AC 256 ms
25,452 KB
testcase_48 AC 250 ms
25,448 KB
testcase_49 AC 255 ms
25,488 KB
testcase_50 AC 63 ms
11,308 KB
testcase_51 AC 7 ms
6,676 KB
testcase_52 AC 144 ms
19,704 KB
testcase_53 AC 130 ms
18,384 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 191 ms
23,576 KB
testcase_56 AC 186 ms
23,224 KB
testcase_57 AC 191 ms
23,708 KB
testcase_58 AC 187 ms
23,580 KB
testcase_59 AC 183 ms
23,240 KB
testcase_60 AC 162 ms
25,820 KB
testcase_61 AC 160 ms
24,176 KB
testcase_62 AC 163 ms
28,748 KB
testcase_63 AC 164 ms
34,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N;
vector<vector<int>> adj;

vector<int> order;
vector<vector<int>> children;

void dfs(int x, int p = -1) {
    order.push_back(x);
    for (auto &&c : adj[x]) {
        if (c == p) continue;
        dfs(c, x);
        children[x].push_back(c);
    }
}

int main() {
    cin >> N;
    adj.resize(N);
    children.resize(N);

    for (int i = 0; i < N - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(0);

    vector<pair<int, int>> dp(N, {0, 1});

    reverse(order.begin(), order.end());
    for (auto &&i : order) {
        for (auto &&j : children[i]) {
            dp[i] = {
                dp[i].first + max(dp[j].first, dp[j].second),
                dp[i].second + dp[j].first,
            };
        }
    }

    reverse(order.begin(), order.end());
    for (auto &&i : order) {
        for (auto &&j : children[i]) {
            pair<int, int> dp_par = {
                dp[i].first - max(dp[j].first, dp[j].second),
                dp[i].second - dp[j].first,
            };
            dp[j] = {
                dp[j].first + max(dp_par.first, dp_par.second),
                dp[j].second + dp_par.first,
            };
        }
    }

    int ans = numeric_limits<int>::max();
    for (auto &&[x, y] : dp) {
        ans = min(ans, y);
    }
    cout << ans << endl;
}
0