結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 nu50218nu50218
提出日時 2024-02-13 21:10:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,378 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 219,772 KB
実行使用メモリ 83,136 KB
最終ジャッジ日時 2024-03-15 20:52:11
合計ジャッジ時間 48,360 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
14,556 KB
testcase_01 AC 7 ms
14,556 KB
testcase_02 AC 6 ms
14,556 KB
testcase_03 AC 6 ms
14,556 KB
testcase_04 AC 7 ms
14,556 KB
testcase_05 AC 726 ms
70,464 KB
testcase_06 AC 719 ms
69,824 KB
testcase_07 AC 713 ms
69,824 KB
testcase_08 AC 723 ms
70,592 KB
testcase_09 AC 723 ms
70,208 KB
testcase_10 AC 1,850 ms
55,808 KB
testcase_11 AC 1,857 ms
55,808 KB
testcase_12 AC 412 ms
83,136 KB
testcase_13 AC 1,860 ms
69,452 KB
testcase_14 AC 1,869 ms
64,972 KB
testcase_15 AC 1,869 ms
70,820 KB
testcase_16 AC 753 ms
67,740 KB
testcase_17 AC 760 ms
67,740 KB
testcase_18 AC 458 ms
48,520 KB
testcase_19 AC 565 ms
54,628 KB
testcase_20 AC 173 ms
28,584 KB
testcase_21 AC 631 ms
58,996 KB
testcase_22 AC 36 ms
18,396 KB
testcase_23 AC 7 ms
14,684 KB
testcase_24 AC 7 ms
14,556 KB
testcase_25 AC 7 ms
14,556 KB
testcase_26 AC 7 ms
14,556 KB
testcase_27 AC 7 ms
14,684 KB
testcase_28 AC 1,850 ms
55,888 KB
testcase_29 AC 629 ms
78,644 KB
testcase_30 AC 6 ms
14,556 KB
testcase_31 AC 7 ms
14,556 KB
testcase_32 AC 7 ms
14,556 KB
testcase_33 AC 8 ms
14,556 KB
testcase_34 AC 8 ms
14,556 KB
testcase_35 AC 6 ms
14,556 KB
testcase_36 AC 7 ms
14,556 KB
testcase_37 AC 7 ms
14,684 KB
testcase_38 AC 7 ms
14,684 KB
testcase_39 AC 9 ms
14,940 KB
testcase_40 AC 11 ms
15,324 KB
testcase_41 AC 17 ms
16,220 KB
testcase_42 AC 29 ms
17,756 KB
testcase_43 AC 57 ms
20,956 KB
testcase_44 AC 141 ms
27,548 KB
testcase_45 AC 318 ms
40,616 KB
testcase_46 AC 687 ms
66,756 KB
testcase_47 AC 724 ms
67,596 KB
testcase_48 AC 733 ms
67,724 KB
testcase_49 AC 722 ms
67,724 KB
testcase_50 AC 1,822 ms
30,180 KB
testcase_51 AC 1,520 ms
16,348 KB
testcase_52 AC 1,846 ms
48,768 KB
testcase_53 AC 1,837 ms
46,088 KB
testcase_54 AC 1,349 ms
15,964 KB
testcase_55 AC 1,927 ms
55,932 KB
testcase_56 AC 1,909 ms
55,912 KB
testcase_57 AC 1,920 ms
55,916 KB
testcase_58 AC 1,917 ms
55,924 KB
testcase_59 AC 1,908 ms
55,912 KB
testcase_60 WA -
testcase_61 AC 332 ms
67,832 KB
testcase_62 AC 292 ms
70,592 KB
testcase_63 AC 312 ms
75,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 次数が大きい順に試していくヒューリスティック + メモ化
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> adj;
unordered_map<int, pair<int, int>> memo[200100];

// 木DPを行う
pair<int, int> rec(int r, int par = -1) {
    if (memo[r][par] != pair<int, int>{0, 0}) return memo[r][par];
    
    int ret0 = 0;
    int ret1 = 1;

    for (auto &&c : adj[r]) {
        if (c == par) continue;
        auto [dp0, dp1] = rec(c, r);
        ret0 += max(dp0, dp1);
        ret1 += dp0;
    }

    return memo[r][par] = {ret0, ret1};
}

int main() {
    auto start = chrono::system_clock::now();

    int N;
    cin >> N;

    adj.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);
    }

    vector<pair<int, int>> order;
    for (int i = 0; i < N; i++) {
        order.push_back({adj[i].size(), i});
    }
    sort(order.begin(), order.end(), greater<pair<int, int>>());

    int ans = numeric_limits<int>::max();

    for (auto &&[_, r] : order) {
        auto now = chrono::system_clock::now();
        auto ms = chrono::duration_cast<std::chrono::milliseconds>(now - start).count();

        if (ms > 1800) break;

        auto [dp0, dp1] = rec(r);

        ans = min(ans, dp1);
    }

    cout << ans << endl;
}
0