結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー ukunichiaukunichia
提出日時 2019-03-28 15:26:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 904 bytes
コンパイル時間 1,878 ms
コンパイル使用メモリ 166,596 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-21 00:43:27
合計ジャッジ時間 6,611 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 91 ms
8,448 KB
testcase_08 AC 45 ms
7,296 KB
testcase_09 AC 54 ms
7,620 KB
testcase_10 AC 40 ms
6,952 KB
testcase_11 AC 141 ms
9,668 KB
testcase_12 AC 146 ms
9,728 KB
testcase_13 AC 146 ms
9,600 KB
testcase_14 AC 146 ms
9,740 KB
testcase_15 AC 155 ms
9,792 KB
testcase_16 AC 159 ms
9,984 KB
testcase_17 AC 158 ms
9,952 KB
testcase_18 AC 213 ms
10,040 KB
testcase_19 AC 215 ms
10,112 KB
testcase_20 AC 208 ms
10,368 KB
testcase_21 AC 201 ms
10,056 KB
20evil_special_uni1.txt AC 167 ms
10,112 KB
20evil_special_uni2.txt AC 159 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int N;
vector<int> G[100010];
int dp[100010];
int dp2[100010];
bool ans[100010];

void dfs(int u, int p) {
  dp[u] = 1;
  for(int v : G[u]) {
    if(v == p) continue;
    dfs(v, u);
    dp[u] &= !dp[v];
  }
}

void dfs2(int u, int p, int carryin) {
  dp2[u] = 0;
  for(int v : G[u]) {
    if(v == p) dp2[u] += !carryin;
    else dp2[u] += !dp[v];
  }
  //cout<<u<<" "<<dp[u]<<" "<<G[u].size()<<endl;
  if(dp2[u] == G[u].size()) ans[u] = true;
  for(int v : G[u]) {
    if(v == p) continue;
    dfs2(v, u, dp2[u]-!dp[v]==G[u].size()-1);
  }
}


int main() {
  cin >> N;
  for(int i = 0; i < N-1; ++i) {
    int a, b;
    cin >> a >> b; --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(0, -1);
  dfs2(0, -1, 0);
  cout << count(ans, ans+N, true) << endl;
  for(int i = 0; i < N; ++i) {
    if(ans[i]) cout << i+1 << endl;
  }

  return 0;
}
0