結果
| 問題 | No.277 根掘り葉掘り |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-01-18 07:08:27 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 212 ms / 3,000 ms |
| + 509µs | |
| コード長 | 786 bytes |
| 記録 | |
| コンパイル時間 | 239 ms |
| コンパイル使用メモリ | 96,364 KB |
| 実行使用メモリ | 100,864 KB |
| 最終ジャッジ日時 | 2026-07-25 10:30:57 |
| 合計ジャッジ時間 | 5,274 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
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]))