結果
問題 | No.768 Tapris and Noel play the game on Treeone |
ユーザー | lumc_ |
提出日時 | 2018-06-16 01:34:16 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 216 ms / 2,000 ms |
コード長 | 1,537 bytes |
コンパイル時間 | 1,379 ms |
コンパイル使用メモリ | 164,424 KB |
実行使用メモリ | 35,504 KB |
最終ジャッジ日時 | 2023-10-25 22:22:01 |
合計ジャッジ時間 | 5,264 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
31,840 KB |
testcase_01 | AC | 9 ms
31,840 KB |
testcase_02 | AC | 9 ms
29,792 KB |
testcase_03 | AC | 10 ms
31,868 KB |
testcase_04 | AC | 10 ms
31,876 KB |
testcase_05 | AC | 9 ms
31,872 KB |
testcase_06 | AC | 10 ms
31,860 KB |
testcase_07 | AC | 89 ms
33,840 KB |
testcase_08 | AC | 54 ms
32,928 KB |
testcase_09 | AC | 60 ms
33,096 KB |
testcase_10 | AC | 47 ms
32,828 KB |
testcase_11 | AC | 136 ms
34,928 KB |
testcase_12 | AC | 139 ms
34,972 KB |
testcase_13 | AC | 133 ms
34,872 KB |
testcase_14 | AC | 132 ms
34,868 KB |
testcase_15 | AC | 142 ms
35,064 KB |
testcase_16 | AC | 142 ms
35,092 KB |
testcase_17 | AC | 147 ms
35,136 KB |
testcase_18 | AC | 216 ms
35,484 KB |
testcase_19 | AC | 215 ms
35,504 KB |
testcase_20 | AC | 210 ms
35,452 KB |
testcase_21 | AC | 200 ms
35,184 KB |
20evil_special_uni1.txt | AC | 162 ms
35,272 KB |
20evil_special_uni2.txt | AC | 155 ms
35,064 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; const int N = 1e6; vector<int> g[N]; int dp0[N]; int dp1[N]; int n; bool t[N]; // 毎度確保するほどでもない一時変数 bool ans[N]; bool dfs0(int v, int p) { bool flag = 0; for(int u : g[v]) if(u != p) if(dfs0(u, v)) flag = 1; // dp0は頂点rを根とした率直な答え return dp0[v] = !flag; } void dfs1(int v, int p) { int cnt = 0; ans[v] = 1; for(int u : g[v]) if(u != p) { t[u] = dp0[u]; if(t[u]) ans[v] = 0, cnt++; } if(p != -1) { t[v] = dp1[v]; if(t[v]) ans[v] = 0, cnt++; } // dp1はuをキャンセルした, 頂点rを根とする, vでの答え // cnt >= 2 ならどの頂点uをキャンセルしても他に t[x] == 1 となる x (!= u) あり // cnt == 1 なら t[u] == 1 なる 頂点u をキャンセル時のみほかに 1 なし for(int u : g[v]) if(u != p) { dp1[u] = !(cnt >= 2 || (cnt == 1 && t[u] == 0)); dfs1(u, v); } } int main() { cin >> n; if(n == 1) return (cout << 1 << endl << 1 << endl, 0); if(n == 2) return (cout << 0 << endl, 0); for(int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].emplace_back(b); g[b].emplace_back(a); } int r = 0; for(int i = 1; i < n; i++) if(g[i].size() >= 2) r = i; // 次数が2以上であるような頂点rを選ぶ vector<int> res; res.reserve(n); dfs0(r, -1); dfs1(r, -1); for(int i = 0; i < n; i++) if(ans[i]) res.emplace_back(i); cout << res.size() << endl; for(int el : res) cout << el + 1 << endl; }