結果

問題 No.277 根掘り葉掘り
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-01-18 07:08:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 350 ms / 3,000 ms
コード長 786 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 86,684 KB
実行使用メモリ 96,256 KB
最終ジャッジ日時 2023-09-02 03:46:08
合計ジャッジ時間 6,819 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
73,028 KB
testcase_01 AC 94 ms
72,748 KB
testcase_02 AC 93 ms
73,000 KB
testcase_03 AC 98 ms
72,936 KB
testcase_04 AC 98 ms
72,880 KB
testcase_05 AC 94 ms
73,116 KB
testcase_06 AC 93 ms
72,968 KB
testcase_07 AC 97 ms
72,768 KB
testcase_08 AC 102 ms
73,144 KB
testcase_09 AC 223 ms
92,896 KB
testcase_10 AC 237 ms
96,256 KB
testcase_11 AC 253 ms
95,348 KB
testcase_12 AC 263 ms
94,248 KB
testcase_13 AC 294 ms
95,328 KB
testcase_14 AC 289 ms
95,348 KB
testcase_15 AC 273 ms
94,996 KB
testcase_16 AC 350 ms
95,112 KB
testcase_17 AC 261 ms
95,488 KB
testcase_18 AC 345 ms
95,580 KB
testcase_19 AC 266 ms
95,316 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