結果

問題 No.806 木を道に
ユーザー finefine
提出日時 2019-03-22 22:38:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 665 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 172,220 KB
実行使用メモリ 13,824 KB
最終ジャッジ日時 2024-09-19 06:04:37
合計ジャッジ時間 3,003 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int solve(int cur, int par, vector< vector<int> >& g) {
    int res = 0;
    int cnt = 0;
    for (int nex : g[cur]) {
        if (nex == par) continue;
        res += solve(nex, cur, g);
        cnt++;
    }
    res += max(cnt - 1 - (cur == 0), 0);
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector< vector<int> > g(n);
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    cout << solve(0, -1, g) << endl;
    return 0;
}
0