結果

問題 No.277 根掘り葉掘り
ユーザー qibqib
提出日時 2023-01-01 00:13:08
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 481 ms / 3,000 ms
コード長 693 bytes
コンパイル時間 287 ms
使用メモリ 103,048 KB
最終ジャッジ日時 2023-01-01 00:13:17
合計ジャッジ時間 9,000 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 94 ms
76,072 KB
testcase_01 AC 93 ms
76,084 KB
testcase_02 AC 94 ms
75,816 KB
testcase_03 AC 97 ms
76,668 KB
testcase_04 AC 99 ms
76,712 KB
testcase_05 AC 107 ms
76,740 KB
testcase_06 AC 112 ms
76,748 KB
testcase_07 AC 99 ms
76,620 KB
testcase_08 AC 93 ms
76,184 KB
testcase_09 AC 393 ms
98,140 KB
testcase_10 AC 365 ms
103,048 KB
testcase_11 AC 481 ms
100,552 KB
testcase_12 AC 418 ms
97,992 KB
testcase_13 AC 475 ms
100,412 KB
testcase_14 AC 448 ms
100,024 KB
testcase_15 AC 460 ms
100,504 KB
testcase_16 AC 441 ms
100,700 KB
testcase_17 AC 436 ms
100,780 KB
testcase_18 AC 460 ms
99,360 KB
testcase_19 AC 439 ms
100,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

INF = 1 << 60

n = int(input())

g = [[] for _ in range(n)]
for _ in range(n - 1):
  x, y = map(int, input().split())
  x -= 1
  y -= 1
  g[x].append(y)
  g[y].append(x)

src = 0
r = [INF for _ in range(n)]
r[src] = 0
dq = deque([src])
while len(dq) > 0:
  cur = dq.popleft()
  for nxt in g[cur]:
    if r[nxt] < INF:
      continue

    r[nxt] = r[cur] + 1
    dq.append(nxt)

l = [INF for _ in range(n)]
for v in range(n):
  if len(g[v]) == 1:
    l[v] = 0
    dq.append(v)

while len(dq) > 0:
  cur = dq.popleft()
  for nxt in g[cur]:
    if l[nxt] < INF:
      continue

    l[nxt] = l[cur] + 1
    dq.append(nxt)

for i in range(n):
  print(min(l[i], r[i]))
0