結果
問題 |
No.768 Tapris and Noel play the game on Treeone
|
ユーザー |
|
提出日時 | 2018-12-16 02:02:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 112 ms / 2,000 ms |
コード長 | 1,304 bytes |
コンパイル時間 | 904 ms |
コンパイル使用メモリ | 83,352 KB |
最終ジャッジ日時 | 2025-01-06 19:36:35 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#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; }