結果

問題 No.277 根掘り葉掘り
ユーザー qibqib
提出日時 2022-12-31 03:49:00
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 702 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 109,912 KB
最終ジャッジ日時 2023-08-17 07:27:55
合計ジャッジ時間 6,180 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,596 KB
testcase_01 AC 94 ms
71,732 KB
testcase_02 WA -
testcase_03 AC 94 ms
71,892 KB
testcase_04 AC 96 ms
71,632 KB
testcase_05 AC 94 ms
71,796 KB
testcase_06 AC 96 ms
71,452 KB
testcase_07 AC 97 ms
71,744 KB
testcase_08 AC 93 ms
71,500 KB
testcase_09 AC 292 ms
99,448 KB
testcase_10 AC 274 ms
109,912 KB
testcase_11 AC 312 ms
94,168 KB
testcase_12 AC 304 ms
96,312 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