結果

問題 No.277 根掘り葉掘り
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-31 07:27:47
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 237 ms / 3,000 ms
コード長 922 bytes
コンパイル時間 931 ms
使用メモリ 11,244 KB
最終ジャッジ日時 2022-12-31 07:27:53
合計ジャッジ時間 4,747 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
3,512 KB
testcase_01 AC 1 ms
3,544 KB
testcase_02 AC 2 ms
3,508 KB
testcase_03 AC 2 ms
3,352 KB
testcase_04 AC 2 ms
3,456 KB
testcase_05 AC 1 ms
3,352 KB
testcase_06 AC 2 ms
3,344 KB
testcase_07 AC 1 ms
3,408 KB
testcase_08 AC 2 ms
3,492 KB
testcase_09 AC 201 ms
9,420 KB
testcase_10 AC 191 ms
11,008 KB
testcase_11 AC 208 ms
11,244 KB
testcase_12 AC 208 ms
10,436 KB
testcase_13 AC 237 ms
10,476 KB
testcase_14 AC 216 ms
10,544 KB
testcase_15 AC 207 ms
10,320 KB
testcase_16 AC 206 ms
10,496 KB
testcase_17 AC 208 ms
10,424 KB
testcase_18 AC 208 ms
10,176 KB
testcase_19 AC 214 ms
10,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>

using namespace std;

int main(){

    long long N, A, B, Q, from;
    cin >> N;
    vector<vector<long long>> E(N);

    for (int i=0; i < N-1; i++){
        cin >> A >> B;
        A--; B--;
        E[A].push_back(B);
        E[B].push_back(A);
    }

    vector<long long> dist(N, -1);
    queue<long long> que;
    dist[0] = 0;
    que.push(0);

    for (int i=1; i<N; i++){
        if (E[i].size() == 1){
            dist[i] = 0;
            que.push(i);
        }
    }

    while(!que.empty()){
        from = que.front();
        que.pop();
        for (auto to : E[from]){
            if (dist[to] != -1) continue; // already visited
            dist[to] = dist[from] + 1;
            que.push(to);
        }   
    }   

    for (int i=0; i<N; i++) cout << dist[i] << endl;

    return 0;
}
0