結果

問題 No.1817 Reversed Edges
ユーザー titiatitia
提出日時 2022-01-21 22:31:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 70 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 38,920 KB
最終ジャッジ日時 2023-08-17 06:48:00
合計ジャッジ時間 9,789 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,856 KB
testcase_01 AC 15 ms
7,776 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 16 ms
7,908 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 369 ms
34,076 KB
testcase_23 AC 380 ms
33,972 KB
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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
Parent[ROOT]=N # ROOTの親を定めておく.
TOP_SORT=[] # トポロジカルソート

Child=[[] for i in range(N)]

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)

ANS=0
SUMS=[0]*N

for x in TOP_SORT[1:][::-1]:
    y=Parent[x]
    if x>y:
        SUMS[x]+=1
    else:
        ANS+=1
        SUMS[x]-=1

for x in TOP_SORT[1:][::-1]:
    y=Parent[x]

    SUMS[x]+=SUMS[y]

for i in range(N):
    print(ANS+SUMS[i])
0