結果

問題 No.806 木を道に
ユーザー Y17Y17
提出日時 2019-03-22 22:04:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 162,128 KB
実行使用メモリ 12,920 KB
最終ジャッジ日時 2023-10-19 09:12:25
合計ジャッジ時間 3,561 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,360 KB
testcase_01 AC 3 ms
6,360 KB
testcase_02 AC 3 ms
6,364 KB
testcase_03 AC 3 ms
6,364 KB
testcase_04 AC 3 ms
6,360 KB
testcase_05 AC 3 ms
6,364 KB
testcase_06 AC 3 ms
6,364 KB
testcase_07 AC 3 ms
6,360 KB
testcase_08 AC 3 ms
6,360 KB
testcase_09 AC 4 ms
6,364 KB
testcase_10 AC 20 ms
7,156 KB
testcase_11 AC 17 ms
7,068 KB
testcase_12 AC 79 ms
9,412 KB
testcase_13 AC 56 ms
8,564 KB
testcase_14 AC 76 ms
9,240 KB
testcase_15 AC 81 ms
9,448 KB
testcase_16 AC 27 ms
7,476 KB
testcase_17 AC 70 ms
9,028 KB
testcase_18 AC 9 ms
6,660 KB
testcase_19 AC 21 ms
7,192 KB
testcase_20 AC 70 ms
9,024 KB
testcase_21 AC 38 ms
7,868 KB
testcase_22 AC 99 ms
9,944 KB
testcase_23 AC 97 ms
9,932 KB
testcase_24 AC 42 ms
10,684 KB
testcase_25 AC 61 ms
12,920 KB
testcase_26 AC 21 ms
7,412 KB
testcase_27 AC 65 ms
9,840 KB
testcase_28 AC 5 ms
6,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> graph[100010];
int vi[100010] = {};

int dfs(int idx){
    vi[idx] = 1;
    bool flag = false;
    int ans = 0;
    for(int i = 0;i < graph[idx].size();i++){
        if(vi[graph[idx][i]] == 0){
            flag = true;
            ans += dfs(graph[idx][i]);
        }
    }

    if(flag){
        return ans;
    }else{
        return 1;
    }
}

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

    int a, b;
    for(int i = 0;i < n-1;i++){
        cin >> a >> b;
        a--, b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    int used[100010] = {};
    int root = 0;

    bool flag;

    do{
        flag = false;
        used[root] = 1;
        for(int i = 0;i < graph[root].size();i++){
            if(used[graph[root][i]] == 0){
                flag = true;
                root = graph[root][i];
                break;
            }
        }
    }while(flag);

    cout << dfs(root)-1 << endl;
    return 0;
}

0