結果

問題 No.1817 Reversed Edges
ユーザー titiatitia
提出日時 2022-01-21 22:15:14
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
WA  
実行時間 -
コード長 2,668 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 11,248 KB
実行使用メモリ 45,932 KB
最終ジャッジ日時 2023-08-17 06:22:45
合計ジャッジ時間 17,550 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,480 KB
testcase_01 AC 18 ms
8,508 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 16 ms
8,476 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 658 ms
35,912 KB
testcase_23 AC 698 ms
36,004 KB
testcase_24 AC 776 ms
45,932 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

# xとParent[x]をつなぐedgeを考える
# UP[x]は、xをROOTとする部分木に関する値。
# xとつながるnodeのうち、Parent[x]以外をxの子と捉える。

# DOWN[x]はParent[x]をROOTとする部分木に関する値。
# Parent[x]とつながるnodeのうち、x以外をParent[x]の子と捉える。

def compose_calc(x,c,k0,k1):# 子たちの値を合成する
    if c<x:
        return k0+k1+1
    else:
        return k0+k1

unit=0 # 単位元

def final_ans(x,value):# 子たちから計算した値から答えを出す
    return value

for x in TOP_SORT[1:][::-1]:
    if Child[x]==[]:
        UP[x]=0
        continue
    
    k=unit # 子が全て1なら0、それ以外は1
    for c in Child[x]:
        k=compose_calc(x,c,k,UP[c])

    UP[x]=final_ans(x,k)

COMPOSE=[1]*N
# DOWN[x]を求めるときに使う
# Parent[x]について、Parent[Parent[x]]以外からの寄与。
# 各iについて、for c in Child[i]についてUP[c]の値をみて、左右からの累積和を使って計算。

for i in range(N):
    X=[]
    for c in Child[i]:
        X.append(UP[c])

    if X==[]:
        continue

    LEFT=[X[0]]
    for j in range(1,len(X)):
        LEFT.append(compose_calc(i,Child[i][j],LEFT[-1],X[j]))

    RIGHT=1
    for j in range(len(X)-1,-1,-1):
        if j!=0:
            COMPOSE[Child[i][j]]=compose_calc(i,Child[i][j],LEFT[j-1],RIGHT)
        else:
            COMPOSE[Child[i][j]]=RIGHT

        RIGHT=compose_calc(i,Child[i][j],RIGHT,X[j])

for x in TOP_SORT:
    if x==ROOT:
        DOWN[x]=0
        continue

    p=Parent[x]
    
    if p==ROOT:
        k=COMPOSE[x]
    else:
        k=compose_calc(x,p,DOWN[p],COMPOSE[x])

    DOWN[x]=final_ans(x,k)


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

    if i!=ROOT:
        k=compose_calc(i,Parent[i],k,DOWN[i])

    ANS[i]=final_ans(i,k)

for ans in ANS:
    print(ans//2)
0