結果

問題 No.763 Noelちゃんと木遊び
ユーザー miwawa
提出日時 2024-02-23 12:20:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 1,311 ms
コンパイル使用メモリ 93,264 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2025-03-22 10:42:50
合計ジャッジ時間 3,613 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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