結果

問題 No.277 根掘り葉掘り
ユーザー takakintakakin
提出日時 2020-06-22 23:49:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 874 bytes
コンパイル時間 74 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 36,216 KB
最終ジャッジ日時 2023-09-16 19:37:31
合計ジャッジ時間 9,541 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,700 KB
testcase_01 AC 17 ms
8,712 KB
testcase_02 WA -
testcase_03 AC 20 ms
8,632 KB
testcase_04 AC 19 ms
8,780 KB
testcase_05 AC 19 ms
8,700 KB
testcase_06 AC 19 ms
8,636 KB
testcase_07 AC 18 ms
8,652 KB
testcase_08 AC 18 ms
8,672 KB
testcase_09 AC 725 ms
36,216 KB
testcase_10 AC 613 ms
28,272 KB
testcase_11 AC 661 ms
29,984 KB
testcase_12 AC 714 ms
33,308 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
edge=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

inf=float("inf")
R=[-1]*n
R[0]=0
Par=[inf]*n
Par[0]=-1
Deg=[0]*n
Deg[0]=0
Chk=[0]

while Chk:
  c=Chk.pop()
  for next in edge[c]:
    if R[next]==-1:
      R[next]=R[c]+1
      Par[next]=c
      Deg[next]+=1
      Chk.append(next)

from collections import deque
TS=list(v for v in range(n) if Deg[v]==0)
D=deque(TS)
while D:
  v=D.popleft()
  for t in edge[v]:
    if t!=Par[v]:
      Deg[t]-=1
      if Deg[t]==0:
        D.append(t)
        TS.append(t)

Used=[False]*n
C=[inf]*n
for i in reversed(range(n)):
  v=TS[i]
  Used[v]=True
  if C[v]==inf:
    C[v]=0
  for g in edge[v]:
    if not Used[g]:
      C[g]=min(C[g],C[v]+1)

for i in range(n):
  print(min(R[i],C[i]))
0