結果

問題 No.277 根掘り葉掘り
ユーザー しらっ亭しらっ亭
提出日時 2015-09-05 20:16:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 559 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 27,128 KB
最終ジャッジ日時 2023-09-26 08:55:41
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,488 KB
testcase_01 AC 19 ms
8,464 KB
testcase_02 WA -
testcase_03 AC 19 ms
8,480 KB
testcase_04 AC 19 ms
8,600 KB
testcase_05 AC 18 ms
8,564 KB
testcase_06 AC 19 ms
8,644 KB
testcase_07 WA -
testcase_08 AC 20 ms
8,480 KB
testcase_09 WA -
testcase_10 AC 403 ms
27,128 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 #

from collections import defaultdict


def solve():
    N = int(input())

    C = defaultdict(list)
    for i in range(N - 1):
        x, y = map(int, input().split())
        C[x - 1].append(y - 1)

    L = [0] * N
    R = [0] * N

    def dfs(v, r):
        R[v] = r
        L[v] = 0
        if C[v]:
            mi = float('inf')
            for c in C[v]:
                mi = min(mi, dfs(c, r + 1))
            L[v] = mi + 1
        return L[v]
    dfs(0, 0)

    for i in range(N):
        print(min(R[i], L[i]))


if __name__ == '__main__':
    solve()
0