結果

問題 No.763 Noelちゃんと木遊び
ユーザー simansiman
提出日時 2021-08-20 07:11:11
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 4,200 ms
コンパイル使用メモリ 142,960 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-04-21 10:31:44
合計ジャッジ時間 6,125 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
18,944 KB
testcase_01 AC 27 ms
9,216 KB
testcase_02 AC 60 ms
10,624 KB
testcase_03 AC 41 ms
9,728 KB
testcase_04 AC 30 ms
9,216 KB
testcase_05 AC 38 ms
9,728 KB
testcase_06 AC 70 ms
11,136 KB
testcase_07 AC 66 ms
11,080 KB
testcase_08 AC 42 ms
9,928 KB
testcase_09 AC 29 ms
9,216 KB
testcase_10 AC 14 ms
8,576 KB
testcase_11 AC 77 ms
11,264 KB
testcase_12 AC 68 ms
10,880 KB
testcase_13 AC 67 ms
10,880 KB
testcase_14 AC 57 ms
10,460 KB
testcase_15 AC 39 ms
9,672 KB
testcase_16 AC 11 ms
8,348 KB
testcase_17 AC 40 ms
9,856 KB
testcase_18 AC 72 ms
11,044 KB
testcase_19 AC 64 ms
10,868 KB
testcase_20 AC 64 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
const int MAX_N = 200000;
vector<int> E[MAX_N];

void dfs(int u, int p, vector<bool> &used) {
  bool has_child = false;

  for (int v : E[u]) {
    if (v == p) continue;

    dfs(v, u, used);
    if (used[v]) has_child = true;
  }

  if (not has_child) used[u] = true;
}

int main() {
  int N;
  cin >> N;

  int u, v;
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v;
    E[u].push_back(v);
    E[v].push_back(u);
  }

  vector<bool> used(N + 1, false);
  dfs(1, -1, used);

  int ans = 0;

  for (int u = 1; u <= N; ++u) {
    if (used[u]) ++ans;
  }

  cout << ans << endl;

  return 0;
}
0