結果

問題 No.763 Noelちゃんと木遊び
ユーザー miwawamiwawa
提出日時 2024-02-23 12:20:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 95,512 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2024-02-23 12:20:18
合計ジャッジ時間 3,954 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
16,640 KB
testcase_01 AC 30 ms
6,676 KB
testcase_02 AC 77 ms
7,680 KB
testcase_03 AC 52 ms
6,676 KB
testcase_04 AC 33 ms
6,676 KB
testcase_05 AC 46 ms
6,676 KB
testcase_06 AC 94 ms
8,576 KB
testcase_07 AC 92 ms
8,448 KB
testcase_08 AC 51 ms
6,676 KB
testcase_09 AC 33 ms
6,676 KB
testcase_10 AC 13 ms
6,676 KB
testcase_11 AC 98 ms
8,704 KB
testcase_12 AC 84 ms
8,064 KB
testcase_13 AC 85 ms
8,064 KB
testcase_14 AC 74 ms
7,552 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 9 ms
6,676 KB
testcase_17 AC 50 ms
6,676 KB
testcase_18 AC 97 ms
8,576 KB
testcase_19 AC 88 ms
8,192 KB
testcase_20 AC 88 ms
8,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

vector<bool> used;
void rec(const Graph &G, int v, int p) {
    // 子頂点の中に採用済みの頂点があるかどうか
    bool exist = false;
    for (auto ch: G[v]) {
        if (ch == p) continue;

        // 再帰的探索
        rec(G, ch, v);
        if (used[ch]) exist = true;
    }

    // 子頂点の中に採用済みの頂点がなければ採用する
    if (!exist) used[v] = true;
}

int main() {
    // 入力
    int N;
    cin >> N;
    Graph G(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    // 頂点 0 を根として探索
    used.assign(N, false);/*assignはもともとの配列のN子分の
    要素を(,)の右側の値に変える。*/
    rec(G, 0,-1);

    // 数える
    int res = 0;
    for (int v = 0; v < N; ++v) {
        if (used[v]) ++res;
    }
    cout << res << endl;
}
0