結果

問題 No.1817 Reversed Edges
ユーザー titia
提出日時 2022-01-21 22:50:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,131 bytes
コンパイル時間 116 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 44,348 KB
最終ジャッジ日時 2024-11-26 01:58:45
合計ジャッジ時間 11,949 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 WA * 19
権限があれば一括ダウンロードができます

ソースコード

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:][::-1]:
    #print(x,Parent[x])
    if Parent[x]>x:
        k=1
    else:
        k=0
    
    for c in Child[x]:
        k+=UP[c]

    UP[x]=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]
    else:
        DOWN[x]=DOWN[p]+UP[p]

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