結果

問題 No.277 根掘り葉掘り
ユーザー nebukuro09nebukuro09
提出日時 2016-10-03 15:22:55
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 805 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 7,072 KB
実行使用メモリ 42,872 KB
最終ジャッジ日時 2024-05-01 11:20:04
合計ジャッジ時間 6,643 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
13,884 KB
testcase_01 AC 12 ms
6,940 KB
testcase_02 AC 13 ms
6,940 KB
testcase_03 AC 12 ms
6,940 KB
testcase_04 AC 18 ms
6,940 KB
testcase_05 AC 15 ms
6,940 KB
testcase_06 AC 15 ms
6,940 KB
testcase_07 AC 15 ms
6,940 KB
testcase_08 AC 12 ms
6,944 KB
testcase_09 AC 679 ms
29,952 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N = input()
rinsetsu = [[] for _ in xrange(N)]
for i in xrange(N-1):
    x, y = map(int, raw_input().split())
    x -= 1
    y -= 1
    rinsetsu[x].append(y)
    rinsetsu[y].append(x)
ans = [float('inf') for _ in xrange(N)]

def dfs(start, happa=False):
    stack = [(start, 0, None)]
    if happa:
        leaves = []
    while len(stack) > 0:
        node, depth, parent = stack.pop()
        ans[node] = min(ans[node], depth)
        if happa and len(rinsetsu[node]) == 1 and parent is not None:
            leaves.append(node)
            continue
        for nn in rinsetsu[node]:
            if nn == parent:
                continue
            stack.append((nn, depth+1, node))
    if happa:
        return leaves

leaves = dfs(0, True)
for leaf in leaves:
    dfs(leaf)
for a in ans:
    print a
0