結果

問題 No.277 根掘り葉掘り
ユーザー nebukuro09nebukuro09
提出日時 2016-10-03 15:23:28
言語 PyPy2
(7.3.15)
結果
TLE  
実行時間 -
コード長 805 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 76,704 KB
実行使用メモリ 293,292 KB
最終ジャッジ日時 2024-05-01 11:20:14
合計ジャッジ時間 6,129 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
82,740 KB
testcase_01 AC 79 ms
75,552 KB
testcase_02 AC 81 ms
75,432 KB
testcase_03 AC 81 ms
74,788 KB
testcase_04 AC 94 ms
77,088 KB
testcase_05 AC 90 ms
77,560 KB
testcase_06 AC 87 ms
77,192 KB
testcase_07 AC 86 ms
77,080 KB
testcase_08 AC 76 ms
75,184 KB
testcase_09 AC 306 ms
91,540 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