結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2016-10-03 16:15:50 |
言語 | PyPy2 (7.3.15) |
結果 |
AC
|
実行時間 | 381 ms / 3,000 ms |
コード長 | 1,095 bytes |
コンパイル時間 | 166 ms |
コンパイル使用メモリ | 76,564 KB |
実行使用メモリ | 114,816 KB |
最終ジャッジ日時 | 2024-11-21 15:18:38 |
合計ジャッジ時間 | 5,832 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
from collections import dequeN = input()rinsetsu = [[] for _ in xrange(N)]for i in xrange(N-1):x, y = map(int, raw_input().split())x -= 1y -= 1rinsetsu[x].append(y)rinsetsu[y].append(x)ans = [float('inf') for _ in xrange(N)]def dfs(start):stack = [(start, None)]leaves = []while len(stack) > 0:node, parent = stack.pop()if len(rinsetsu[node]) == 1 and parent is not None:leaves.append(node)continuefor nn in rinsetsu[node]:if nn == parent:continuestack.append((nn, node))return leavesdef bfs(starts):q = deque()for s in starts:q.append((s, 0))visited = [False for _ in xrange(N)]while len(q) > 0:node, depth = q.popleft()if visited[node]:continuevisited[node] = Trueans[node] = min(ans[node], depth)for nn in rinsetsu[node]:if not visited[nn]:q.append((nn, depth+1))leaves = dfs(0) + [0]bfs(leaves)for a in ans:print a