結果

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

ソースコード

diff #

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

int N;
vector<vector<int>> G;
vector<int> book;

int maine(int idx, int par, int depth) {
    int ret = N;
    if (G[idx].size() == 1)
        ret = 0;
    for (auto&& nxt : G[idx]) {
        if (nxt == par)
            continue;
        ret = min(ret, maine(nxt, idx, depth + 1) + 1);
    }
    book[idx] = min(depth, ret);
    return ret;
}

int main() {
    cin >> N;
    G.resize(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);
    }
    book.resize(N);
    maine(0, -1, 0);
    for (auto&& a : book) {
        cout << a << endl;
    }
}
0