結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー lumc_lumc_
提出日時 2018-06-15 23:56:41
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,563 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 164,412 KB
実行使用メモリ 35,548 KB
最終ジャッジ日時 2023-10-25 21:48:49
合計ジャッジ時間 5,488 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
31,892 KB
testcase_01 AC 12 ms
31,892 KB
testcase_02 WA -
testcase_03 AC 12 ms
31,920 KB
testcase_04 AC 14 ms
31,928 KB
testcase_05 AC 13 ms
31,924 KB
testcase_06 AC 13 ms
31,912 KB
testcase_07 AC 100 ms
33,892 KB
testcase_08 AC 56 ms
32,980 KB
testcase_09 AC 65 ms
33,148 KB
testcase_10 AC 51 ms
32,880 KB
testcase_11 AC 154 ms
34,980 KB
testcase_12 AC 153 ms
35,024 KB
testcase_13 AC 150 ms
34,924 KB
testcase_14 AC 157 ms
34,920 KB
testcase_15 AC 158 ms
35,116 KB
testcase_16 AC 161 ms
35,144 KB
testcase_17 AC 171 ms
35,188 KB
testcase_18 AC 223 ms
35,528 KB
testcase_19 AC 221 ms
35,548 KB
testcase_20 AC 217 ms
35,504 KB
testcase_21 AC 206 ms
35,236 KB
20evil_special_uni1.txt AC 178 ms
35,324 KB
20evil_special_uni2.txt AC 168 ms
35,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 << 2 << endl << 1 << endl << 2 << 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;
}

0