結果

問題 No.2677 Minmax Independent Set
ユーザー ゼットゼット
提出日時 2024-03-15 23:21:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 2,970 bytes
コンパイル時間 1,590 ms
コンパイル使用メモリ 112,108 KB
実行使用メモリ 83,912 KB
最終ジャッジ日時 2024-03-15 23:21:32
合計ジャッジ時間 24,393 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 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 617 ms
77,556 KB
testcase_06 AC 591 ms
77,556 KB
testcase_07 AC 618 ms
77,556 KB
testcase_08 AC 587 ms
77,556 KB
testcase_09 AC 617 ms
77,556 KB
testcase_10 AC 632 ms
83,264 KB
testcase_11 AC 650 ms
83,264 KB
testcase_12 AC 437 ms
77,556 KB
testcase_13 AC 697 ms
80,672 KB
testcase_14 AC 666 ms
81,716 KB
testcase_15 AC 730 ms
80,376 KB
testcase_16 AC 646 ms
77,884 KB
testcase_17 AC 678 ms
77,888 KB
testcase_18 AC 391 ms
50,916 KB
testcase_19 AC 469 ms
59,612 KB
testcase_20 AC 151 ms
23,280 KB
testcase_21 AC 550 ms
65,648 KB
testcase_22 AC 32 ms
8,960 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 2 ms
6,676 KB
testcase_28 AC 670 ms
83,912 KB
testcase_29 AC 604 ms
77,492 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 5 ms
6,676 KB
testcase_40 AC 7 ms
6,676 KB
testcase_41 AC 13 ms
6,676 KB
testcase_42 AC 27 ms
7,936 KB
testcase_43 AC 62 ms
12,544 KB
testcase_44 AC 144 ms
21,680 KB
testcase_45 AC 302 ms
39,872 KB
testcase_46 AC 624 ms
76,384 KB
testcase_47 AC 661 ms
77,624 KB
testcase_48 AC 648 ms
77,624 KB
testcase_49 AC 657 ms
77,628 KB
testcase_50 AC 207 ms
33,836 KB
testcase_51 AC 12 ms
6,676 KB
testcase_52 AC 519 ms
70,056 KB
testcase_53 AC 506 ms
64,848 KB
testcase_54 AC 10 ms
6,676 KB
testcase_55 AC 621 ms
81,904 KB
testcase_56 AC 644 ms
82,372 KB
testcase_57 AC 634 ms
81,888 KB
testcase_58 AC 648 ms
81,892 KB
testcase_59 AC 627 ms
82,372 KB
testcase_60 AC 336 ms
78,612 KB
testcase_61 AC 320 ms
78,048 KB
testcase_62 AC 298 ms
77,556 KB
testcase_63 AC 309 ms
78,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <map>

using namespace std;

int main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b;
        cin >> a >> b;
        G[a - 1].push_back(b - 1);
        G[b - 1].push_back(a - 1);
    }

    vector<vector<int>> dp1(N, vector<int>());
    vector<vector<int>> dp2(N, vector<int>());
    vector<vector<int>> c(N, vector<int>(2, 0));
    vector<int> dist(N, -1);
    dist[0] = 0;
    deque<int> S;
    S.push_back(0);
    while (!S.empty()) {
        int x = S.back();
        S.pop_back();
        for (int y : G[x]) {
            if (dist[y] >= 0) continue;
            dist[y] = dist[x] + 1;
            S.push_back(y);
        }
    }

    vector<pair<int, int>> L;
    for (int i = 0; i < N; ++i) {
        L.push_back(make_pair(dist[i], i));
    }
    sort(L.rbegin(), L.rend());
    for (int i = 0; i < N; ++i) {
        int x = L[i].second;
        int k1 = 0, k2 = 1;
        vector<int> u1(G[x].size(), 0), u2(G[x].size(), 0);
        for (int j = 0; j < G[x].size(); ++j) {
            int y = G[x][j];
            if (dist[y] < dist[x]) continue;
            u1[j] = c[y][0];
            u2[j] = max(c[y][0], c[y][1]);
            k1 += u2[j];
            k2 += u1[j];
        }
        dp1[x] = u1;
        dp2[x] = u2;
        c[x][0] = k1;
        c[x][1] = k2;
    }

    S.push_back(0);
    vector<map<int, int>> T(N);
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < G[i].size(); ++j) {
            T[i][G[i][j]] = j;
        }
    }

    while (!S.empty()) {
        int x = S.back();
        S.pop_back();
        vector<int> l1(G[x].size(), 0), r1(G[x].size(), 0), l2(G[x].size(), 0), r2(G[x].size(), 0);
        for (int j = 0; j < G[x].size(); ++j) {
            l1[j] = dp1[x][j];
            l2[j] = dp2[x][j];
            r1[j] = dp1[x][j];
            r2[j] = dp2[x][j];
        }
        for (int j = 1; j < G[x].size(); ++j) {
            l1[j] += l1[j - 1];
            l2[j] += l2[j - 1];
        }
        for (int j = G[x].size() - 2; j >= 0; --j) {
            r1[j] += r1[j + 1];
            r2[j] += r2[j + 1];
        }
        for (int j = 0; j < G[x].size(); ++j) {
            int y = G[x][j];
            if (dist[y] < dist[x]) continue;
            int w1 = 0, w2 = 1;
            if (j > 0) {
                w1 += l2[j - 1];
                w2 += l1[j - 1];
            }
            if (j < G[x].size() - 1) {
                w1 += r2[j + 1];
                w2 += r1[j + 1];
            }
            int pos = T[y][x];
            dp1[y][pos] = w1;
            dp2[y][pos] = max(w2, w1);
            S.push_back(y);
        }
    }

    int result = 1e8;
    for (int i = 0; i < N; ++i) {
        int ans = 1;
        for (int val : dp1[i]) {
            ans += val;
        }
        result = min(result, ans);
    }
    cout << result << endl;

    return 0;
}
0