結果

問題 No.277 根掘り葉掘り
ユーザー nebukuro09
提出日時 2016-10-03 15:22:55
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 805 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 59,796 KB
最終ジャッジ日時 2024-11-21 15:16:47
合計ジャッジ時間 42,696 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 8 TLE * 10
権限があれば一括ダウンロードができます

ソースコード

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