結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー lumc_lumc_
提出日時 2018-03-12 01:24:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 937 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 178,904 KB
実行使用メモリ 35,076 KB
最終ジャッジ日時 2023-10-25 21:49:29
合計ジャッジ時間 8,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,456 KB
testcase_01 AC 6 ms
11,456 KB
testcase_02 AC 5 ms
11,456 KB
testcase_03 AC 7 ms
11,624 KB
testcase_04 AC 8 ms
11,676 KB
testcase_05 AC 7 ms
11,644 KB
testcase_06 AC 6 ms
11,584 KB
testcase_07 AC 195 ms
25,740 KB
testcase_08 AC 96 ms
18,936 KB
testcase_09 AC 113 ms
20,348 KB
testcase_10 AC 76 ms
18,248 KB
testcase_11 AC 333 ms
33,648 KB
testcase_12 AC 334 ms
33,856 KB
testcase_13 AC 324 ms
33,184 KB
testcase_14 AC 317 ms
33,120 KB
testcase_15 AC 345 ms
34,552 KB
testcase_16 AC 351 ms
34,608 KB
testcase_17 AC 350 ms
35,076 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
20evil_special_uni1.txt -- -
20evil_special_uni2.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

vector<int> g[100000];
int N; 
unordered_map<int, bool> dp[100000];

bool dfs(int v, int p) {
  if(dp[v].count(p)) return dp[v][p];
  bool flag = 0;
  for(int u : g[v]) if(u != p) flag |= dfs(u, v);
  return dp[v][p] = !flag;
}

bool isTree() {
  bool flag[N] = {};
  function<void(int, int)> check = [&](int v, int p) {
    flag[v] = 1;
    for(int u : g[v]) if(u != p) check(u, v);
  };
  check(0, -1);
  bool all = 1;
  for(int i = 0; i < N; i++) all &= flag[i];
  return all;
}

int main() {
  cin >> N;
  for(int i = 0; i < N - 1; i++) {
    int a, b; cin >> a >> b; a--; b--;
    assert(0 <= a && a < N && 0 <= b && b < N);
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  assert(isTree());
  
  vector<int> ans; ans.reserve(N);
  for(int i = 0; i < N; i++)
    if(dfs(i, -1)) ans.emplace_back(i);

  cout << ans.size() << endl;
  for(int el : ans) cout << el + 1 << endl;
}
0