結果
問題 | No.768 Tapris and Noel play the game on Treeone |
ユーザー |
|
提出日時 | 2018-12-16 02:00:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 669 ms / 2,000 ms |
コード長 | 1,540 bytes |
コンパイル時間 | 922 ms |
コンパイル使用メモリ | 84,276 KB |
最終ジャッジ日時 | 2025-01-06 19:36:28 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>#define show(x) std::cerr << #x << " = " << (x) << std::endltemplate <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;}