結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 nu50218nu50218
提出日時 2023-12-01 01:34:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,436 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 210,240 KB
実行使用メモリ 40,016 KB
最終ジャッジ日時 2024-03-15 20:51:06
合計ジャッジ時間 11,573 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 227 ms
26,260 KB
testcase_06 AC 228 ms
26,408 KB
testcase_07 AC 225 ms
26,536 KB
testcase_08 AC 221 ms
26,960 KB
testcase_09 AC 222 ms
27,428 KB
testcase_10 AC 161 ms
24,236 KB
testcase_11 AC 162 ms
24,240 KB
testcase_12 AC 247 ms
40,016 KB
testcase_13 AC 234 ms
29,396 KB
testcase_14 AC 217 ms
29,452 KB
testcase_15 AC 245 ms
32,504 KB
testcase_16 AC 257 ms
24,824 KB
testcase_17 AC 260 ms
24,824 KB
testcase_18 AC 157 ms
17,016 KB
testcase_19 AC 189 ms
19,452 KB
testcase_20 AC 57 ms
9,088 KB
testcase_21 AC 212 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 245 ms
35,488 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 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 5 ms
6,676 KB
testcase_41 AC 7 ms
6,676 KB
testcase_42 AC 12 ms
6,676 KB
testcase_43 AC 24 ms
6,676 KB
testcase_44 AC 53 ms
8,832 KB
testcase_45 AC 115 ms
14,244 KB
testcase_46 AC 258 ms
25,056 KB
testcase_47 AC 258 ms
25,432 KB
testcase_48 AC 257 ms
25,428 KB
testcase_49 AC 252 ms
25,452 KB
testcase_50 AC 63 ms
11,308 KB
testcase_51 AC 6 ms
6,676 KB
testcase_52 AC 143 ms
19,704 KB
testcase_53 AC 135 ms
18,384 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 190 ms
23,576 KB
testcase_56 AC 183 ms
23,224 KB
testcase_57 AC 188 ms
23,708 KB
testcase_58 AC 186 ms
23,580 KB
testcase_59 AC 180 ms
23,240 KB
testcase_60 AC 164 ms
25,820 KB
testcase_61 AC 164 ms
24,120 KB
testcase_62 AC 163 ms
27,500 KB
testcase_63 AC 162 ms
31,244 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