結果

問題 No.277 根掘り葉掘り
ユーザー qibqib
提出日時 2022-12-31 03:49:00
言語 PyPy3
(7.3.8)
結果
WA  
実行時間 -
コード長 702 bytes
コンパイル時間 294 ms
使用メモリ 117,796 KB
最終ジャッジ日時 2022-12-31 03:49:10
合計ジャッジ時間 9,100 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 134 ms
76,064 KB
testcase_01 AC 128 ms
76,116 KB
testcase_02 WA -
testcase_03 AC 125 ms
76,644 KB
testcase_04 AC 125 ms
76,668 KB
testcase_05 AC 123 ms
76,624 KB
testcase_06 AC 122 ms
76,636 KB
testcase_07 AC 123 ms
76,708 KB
testcase_08 AC 113 ms
76,080 KB
testcase_09 AC 476 ms
105,732 KB
testcase_10 AC 419 ms
117,796 KB
testcase_11 AC 477 ms
100,584 KB
testcase_12 AC 493 ms
100,080 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = [0 for _ in range(n)]
l = [INF for _ in range(n)]
st = [~ src, src]
par = [None for _ in range(n)]
while len(st) > 0:
  cur = st.pop()
  if cur >= 0:
    for nxt in g[cur]:
      if nxt == par[cur]:
        continue
      r[nxt] = r[cur] + 1
      par[nxt] = cur
      st.append(~ nxt)
      st.append(nxt)
  else:
    if l[~ cur] == INF:
      l[~ cur] = 0
    if not par[~ cur] is None:
      l[par[~ cur]] = min(l[par[~ cur]], l[~ cur] + 1)

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