結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,920 KB
testcase_01 AC 16 ms
7,724 KB
testcase_02 WA -
testcase_03 AC 16 ms
7,920 KB
testcase_04 AC 17 ms
7,748 KB
testcase_05 AC 16 ms
7,752 KB
testcase_06 AC 16 ms
7,800 KB
testcase_07 AC 17 ms
7,880 KB
testcase_08 AC 16 ms
7,884 KB
testcase_09 AC 823 ms
44,884 KB
testcase_10 AC 665 ms
33,944 KB
testcase_11 AC 770 ms
36,984 KB
testcase_12 AC 804 ms
40,476 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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 i in range(N):
    print(min(DEG[i],DEG2[i]))
    

0