結果

問題 No.1817 Reversed Edges
ユーザー titiatitia
提出日時 2022-01-21 23:26:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 799 ms / 2,000 ms
コード長 1,349 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 42,708 KB
最終ジャッジ日時 2023-08-17 08:11:21
合計ジャッジ時間 13,814 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,108 KB
testcase_01 AC 15 ms
8,188 KB
testcase_02 AC 16 ms
8,096 KB
testcase_03 AC 16 ms
8,040 KB
testcase_04 AC 16 ms
8,196 KB
testcase_05 AC 16 ms
8,192 KB
testcase_06 AC 16 ms
8,116 KB
testcase_07 AC 655 ms
37,364 KB
testcase_08 AC 289 ms
22,436 KB
testcase_09 AC 622 ms
35,764 KB
testcase_10 AC 336 ms
24,288 KB
testcase_11 AC 446 ms
29,128 KB
testcase_12 AC 753 ms
41,388 KB
testcase_13 AC 781 ms
41,392 KB
testcase_14 AC 774 ms
41,468 KB
testcase_15 AC 794 ms
41,440 KB
testcase_16 AC 769 ms
41,492 KB
testcase_17 AC 784 ms
41,456 KB
testcase_18 AC 786 ms
41,388 KB
testcase_19 AC 790 ms
41,396 KB
testcase_20 AC 799 ms
41,480 KB
testcase_21 AC 780 ms
41,480 KB
testcase_22 AC 483 ms
35,044 KB
testcase_23 AC 485 ms
35,148 KB
testcase_24 AC 524 ms
42,708 KB
権限があれば一括ダウンロードができます

ソースコード

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)

UP=[0]*N
DOWN=[0]*N

for x in TOP_SORT[::-1]:
    #print(x,Parent[x])
    if x==ROOT:
        k=0
    elif Parent[x]>x:
        k=1
    else:
        k=0
    
    for c in Child[x]:
        k+=UP[c]

    UP[x]=k

COMPOSE=[0]*N
for i in range(N):
    k=0
    for c in Child[i]:
        k+=UP[c]
    COMPOSE[i]=k

for x in TOP_SORT:
    #print(x)
    if x==ROOT:
        DOWN[x]=0
        continue

    p=Parent[x]

    if x>p:
        DOWN[x]=1+DOWN[p]+UP[p]-UP[x]
    else:
        DOWN[x]=DOWN[p]+UP[p]-UP[x]

    if p!=ROOT:
        q=Parent[p]

        if q>p:
            DOWN[x]-=1

ANS=[0]*N
# iをROOTとしたときの値は、
# for c in Child[i]についてのUP[c]とDOWN[i]から求められる。
for i in range(N):
    print(DOWN[i]+COMPOSE[i])
0