結果

問題 No.277 根掘り葉掘り
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-01-18 07:08:27
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 382 ms / 3,000 ms
コード長 786 bytes
コンパイル時間 268 ms
使用メモリ 99,100 KB
最終ジャッジ日時 2023-01-18 07:08:35
合計ジャッジ時間 7,572 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 111 ms
76,756 KB
testcase_01 AC 110 ms
76,820 KB
testcase_02 AC 109 ms
76,904 KB
testcase_03 AC 111 ms
76,848 KB
testcase_04 AC 111 ms
76,680 KB
testcase_05 AC 112 ms
76,764 KB
testcase_06 AC 113 ms
76,732 KB
testcase_07 AC 114 ms
76,656 KB
testcase_08 AC 119 ms
76,632 KB
testcase_09 AC 336 ms
96,264 KB
testcase_10 AC 298 ms
98,820 KB
testcase_11 AC 363 ms
98,104 KB
testcase_12 AC 357 ms
98,344 KB
testcase_13 AC 382 ms
98,672 KB
testcase_14 AC 352 ms
98,492 KB
testcase_15 AC 358 ms
99,100 KB
testcase_16 AC 361 ms
98,056 KB
testcase_17 AC 348 ms
97,980 KB
testcase_18 AC 348 ms
98,288 KB
testcase_19 AC 363 ms
98,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

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

v = deque()
R = [-1]*N
L = [-1]*N
R[0]=0
v.append(0)
while v:
    
    x = v.popleft()
    for ix in e[x]:
        
        if R[ix]!=-1:
            continue
        R[ix] = R[x] + 1
        v.append(ix)
        
for i in range(1,N):
    if len(e[i])==1:
        L[i]=0
        v.append(i)
while v:
    
    x = v.popleft()
    for ix in e[x]:
        
        if L[ix]!=-1:
            continue
        L[ix] = L[x] + 1
        v.append(ix)
for i in range(N):
    print(min(R[i],L[i]))
0