結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-03 15:23:28 |
| 言語 | PyPy2 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 805 bytes |
| コンパイル時間 | 1,676 ms |
| コンパイル使用メモリ | 76,320 KB |
| 実行使用メモリ | 249,004 KB |
| 最終ジャッジ日時 | 2024-11-21 15:17:37 |
| 合計ジャッジ時間 | 43,847 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 TLE * 10 |
ソースコード
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