結果

問題 No.277 根掘り葉掘り
ユーザー rlangevinrlangevin
提出日時 2023-07-31 22:59:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 221 ms / 3,000 ms
コード長 567 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 92,268 KB
最終ジャッジ日時 2024-04-18 22:17:07
合計ジャッジ時間 4,369 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,632 KB
testcase_01 AC 44 ms
53,760 KB
testcase_02 AC 44 ms
53,632 KB
testcase_03 AC 46 ms
54,016 KB
testcase_04 AC 46 ms
53,888 KB
testcase_05 AC 46 ms
53,760 KB
testcase_06 AC 47 ms
54,016 KB
testcase_07 AC 47 ms
54,144 KB
testcase_08 AC 45 ms
53,632 KB
testcase_09 AC 186 ms
89,088 KB
testcase_10 AC 173 ms
92,268 KB
testcase_11 AC 199 ms
90,368 KB
testcase_12 AC 199 ms
90,112 KB
testcase_13 AC 221 ms
91,008 KB
testcase_14 AC 201 ms
90,368 KB
testcase_15 AC 202 ms
89,728 KB
testcase_16 AC 209 ms
89,984 KB
testcase_17 AC 205 ms
89,856 KB
testcase_18 AC 203 ms
89,728 KB
testcase_19 AC 207 ms
90,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[u].append(v)
    G[v].append(u)
    
Q = deque()
dist = [-1] * N
Q.append(0)
dist[0] = 0
for i in range(1, N):
    if len(G[i]) == 1:
        Q.append(i)
        dist[i] = 0
        
while Q:
    u = Q.popleft()
    for v in G[u]:
        if dist[v] != -1:
            continue
        dist[v] = dist[u] + 1
        Q.append(v)
        
for d in dist:
    print(d)    
0