結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー PachicobuePachicobue
提出日時 2018-12-16 02:00:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 661 ms / 2,000 ms
コード長 1,540 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 10,484 KB
最終ジャッジ日時 2023-10-25 21:58:59
合計ジャッジ時間 12,136 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 6 ms
4,348 KB
testcase_04 AC 7 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 350 ms
7,208 KB
testcase_08 AC 182 ms
5,440 KB
testcase_09 AC 220 ms
5,836 KB
testcase_10 AC 168 ms
5,176 KB
testcase_11 AC 582 ms
9,400 KB
testcase_12 AC 566 ms
9,400 KB
testcase_13 AC 538 ms
9,196 KB
testcase_14 AC 529 ms
9,180 KB
testcase_15 AC 587 ms
9,532 KB
testcase_16 AC 595 ms
9,664 KB
testcase_17 AC 620 ms
9,672 KB
testcase_18 AC 658 ms
10,484 KB
testcase_19 AC 661 ms
10,252 KB
testcase_20 AC 626 ms
10,116 KB
testcase_21 AC 599 ms
9,732 KB
20evil_special_uni1.txt AC 617 ms
9,768 KB
20evil_special_uni2.txt AC 584 ms
9,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
template <typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v)
{
    os << "[";
    for (const auto& p : v) { os << p << ","; }
    return (os << "]\n");
}
int main()
{
    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);
    show(one), show(win);
    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() << std::endl;
    std::sort(ans.begin(), ans.end());
    for (const int e : ans) { std::cout << e + 1 << std::endl; }
    return 0;
}
0