結果

問題 No.277 根掘り葉掘り
ユーザー titiatitia
提出日時 2022-09-22 00:10:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 865 ms / 3,000 ms
コード長 886 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 45,052 KB
最終ジャッジ日時 2023-08-23 20:15:02
合計ジャッジ時間 10,637 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,872 KB
testcase_01 AC 16 ms
7,720 KB
testcase_02 AC 16 ms
7,804 KB
testcase_03 AC 16 ms
7,948 KB
testcase_04 AC 16 ms
7,752 KB
testcase_05 AC 17 ms
7,848 KB
testcase_06 AC 16 ms
7,796 KB
testcase_07 AC 16 ms
7,964 KB
testcase_08 AC 16 ms
8,132 KB
testcase_09 AC 857 ms
45,052 KB
testcase_10 AC 725 ms
34,336 KB
testcase_11 AC 837 ms
37,388 KB
testcase_12 AC 853 ms
40,408 KB
testcase_13 AC 855 ms
37,724 KB
testcase_14 AC 817 ms
37,964 KB
testcase_15 AC 847 ms
41,196 KB
testcase_16 AC 865 ms
40,168 KB
testcase_17 AC 865 ms
40,180 KB
testcase_18 AC 830 ms
40,884 KB
testcase_19 AC 860 ms
40,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
E=[[] for i in range(N)]

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

ROOT=0

QUE=[ROOT] 
Parent=[-1]*(N+1)
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N+1)]
TOP_SORT=[] # トポロジカルソート
DEG=[1<<30]*N
DEG[0]=0

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)
            DEG[to]=DEG[x]+1

DEG2=[1<<30]*N
for x in TOP_SORT[::-1]:
    if DEG2[x]==1<<30:
        DEG2[x]=0
    if 0<=Parent[x]<N:
        DEG2[Parent[x]]=min(DEG2[x]+1,DEG2[Parent[x]])

for x in TOP_SORT[1:]:
    DEG2[x]=min(DEG2[x],DEG2[Parent[x]]+1)
    

for i in range(N):
    print(min(DEG[i],DEG2[i]))
0