結果

問題 No.277 根掘り葉掘り
ユーザー maine_honzuki
提出日時 2021-05-11 11:37:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 332 ms / 3,000 ms
コード長 917 bytes
コンパイル時間 3,186 ms
コンパイル使用メモリ 202,900 KB
最終ジャッジ日時 2025-01-21 10:02:51
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/277/
#include <bits/stdc++.h>
using namespace std;


int main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        G[x].emplace_back(y);
        G[y].emplace_back(x);
    }
    vector<int> maine(N);
    vector<bool> seen(N);
    queue<int> que;
    que.push(0);
    seen[0] = true;
    for (int i = 0; i < N; i++) {
        if (G[i].size() == 1) {
            que.push(i);
            seen[i] = true;
        }
    }
    while (!que.empty()) {
        auto idx = que.front();
        que.pop();
        for (auto&& nxt : G[idx]) {
            if (seen[nxt])
                continue;
            seen[nxt] = true;
            maine[nxt] = maine[idx] + 1;
            que.push(nxt);
        }
    }
    for (auto&& x : maine) {
        cout << x << endl;
    }
}
0