結果

問題 No.806 木を道に
ユーザー face4face4
提出日時 2019-03-22 21:51:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 748 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 78,600 KB
実行使用メモリ 9,456 KB
最終ジャッジ日時 2023-10-19 09:01:04
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 19 ms
4,908 KB
testcase_11 AC 16 ms
4,776 KB
testcase_12 AC 77 ms
8,368 KB
testcase_13 AC 55 ms
7,088 KB
testcase_14 AC 73 ms
8,140 KB
testcase_15 AC 78 ms
8,436 KB
testcase_16 AC 26 ms
5,396 KB
testcase_17 AC 69 ms
7,792 KB
testcase_18 AC 8 ms
4,348 KB
testcase_19 AC 19 ms
4,972 KB
testcase_20 AC 67 ms
7,792 KB
testcase_21 AC 36 ms
6,008 KB
testcase_22 AC 93 ms
9,208 KB
testcase_23 AC 92 ms
9,208 KB
testcase_24 AC 38 ms
6,584 KB
testcase_25 AC 57 ms
8,080 KB
testcase_26 AC 21 ms
5,456 KB
testcase_27 AC 70 ms
9,456 KB
testcase_28 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v[n];
    int a, b;
    for(int i = 0; i < n-1; i++){
        cin >> a >> b;
        a--, b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    vector<bool> visit(n, 0);
    int i;
    for(i = 0;;i++) if(v[i].size() == 1)    break;
    queue<int> q;
    q.push(i);
    int ans = 0;
    while(!q.empty()){
        int x = q.front();  q.pop();
        if(visit[x])    continue;
        visit[x] = true;
        if(v[x].size() > 2){
            ans += v[x].size()-2;
        }
        for(int next : v[x]){
            if(!visit[next])    q.push(next);
        }
    }
    cout << ans << endl;
    return 0;
}
0