結果

問題 No.277 根掘り葉掘り
ユーザー nopperinopperi
提出日時 2015-09-05 06:57:07
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 585 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 59,524 KB
実行使用メモリ 8,244 KB
最終ジャッジ日時 2023-09-26 08:44:11
合計ジャッジ時間 4,307 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,624 KB
testcase_01 AC 3 ms
5,708 KB
testcase_02 WA -
testcase_03 AC 4 ms
5,632 KB
testcase_04 AC 3 ms
5,888 KB
testcase_05 WA -
testcase_06 AC 4 ms
5,712 KB
testcase_07 WA -
testcase_08 AC 3 ms
5,628 KB
testcase_09 WA -
testcase_10 AC 174 ms
6,412 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector <int> node[100001];
int cost[100001];

int dfs(int n) {
    if (0 == node[n].size()) {
        return 0;
    }
    int m = 1000000;
    for (int i = 0; i < node[n].size(); i++) {
        m = min(m, 1+dfs(node[n][i]));
    }
    return m;
}

int main(void) {
    int n, x, y;
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        cin >> x >> y;
        node[x].push_back(y);
        cost[y] = 1 + cost[x];
    }
    for (int i = 1; i <= n; i++) {
        cout << min(dfs(i), cost[i]) << endl;
    }
    return 0;
}
0