結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー PachicobuePachicobue
提出日時 2018-12-16 02:02:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,304 bytes
コンパイル時間 989 ms
コンパイル使用メモリ 86,228 KB
実行使用メモリ 10,380 KB
最終ジャッジ日時 2023-10-25 21:59:14
合計ジャッジ時間 3,031 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 30 ms
7,164 KB
testcase_08 AC 16 ms
5,320 KB
testcase_09 AC 20 ms
5,800 KB
testcase_10 AC 15 ms
5,248 KB
testcase_11 AC 60 ms
9,208 KB
testcase_12 AC 50 ms
9,280 KB
testcase_13 AC 50 ms
9,092 KB
testcase_14 AC 56 ms
9,072 KB
testcase_15 AC 60 ms
9,424 KB
testcase_16 AC 61 ms
9,444 KB
testcase_17 AC 64 ms
9,568 KB
testcase_18 AC 50 ms
10,380 KB
testcase_19 AC 52 ms
10,144 KB
testcase_20 AC 59 ms
10,004 KB
testcase_21 AC 51 ms
9,620 KB
20evil_special_uni1.txt AC 53 ms
9,664 KB
20evil_special_uni2.txt AC 55 ms
9,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int N;
    std::cin >> N;
    std::vector<std::vector<int>> g(N);
    for (int i = 0, a, b; i < N - 1; i++) { std::cin >> a >> b, a--, b--, g[a].push_back(b), g[b].push_back(a); }
    std::vector<bool> win(N);
    std::vector<int> one(N, 0);  // 子供に勝木が何個あるか
    auto dp = [&](auto&& self, const int s, const int p) -> bool {
        for (const int to : g[s]) {
            if (to == p) { continue; }
            one[s] += self(self, to, s);
        }
        return win[s] = one[s] == 0;
    };
    dp(dp, 0, -1);
    std::vector<int> ans;
    auto dp2 = [&](auto&& self, const int s, const int p) -> void {
        if (p == -1) {
            if (win[s]) { ans.push_back(s); }
        } else {
            const int pone = one[p] - win[s];
            win[p] = pone == 0, one[s] += win[p];
            if (one[s] == 0) { ans.push_back(s); }
        }
        for (const int to : g[s]) {
            if (to == p) { continue; }
            self(self, to, s);
        }
    };
    dp2(dp2, 0, -1);
    std::cout << ans.size() << "\n";
    std::sort(ans.begin(), ans.end());
    for (const int e : ans) { std::cout << e + 1 << "\n"; }
    return 0;
}
0