結果

問題 No.2598 Kadomatsu on Tree
ユーザー titiatitia
提出日時 2024-01-01 17:41:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 572 ms / 2,000 ms
コード長 1,437 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 145,920 KB
最終ジャッジ日時 2024-01-01 17:41:34
合計ジャッジ時間 22,525 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 34 ms
53,460 KB
testcase_02 AC 34 ms
53,460 KB
testcase_03 AC 34 ms
53,460 KB
testcase_04 AC 34 ms
53,460 KB
testcase_05 AC 34 ms
53,460 KB
testcase_06 AC 35 ms
53,460 KB
testcase_07 AC 39 ms
53,460 KB
testcase_08 AC 34 ms
53,460 KB
testcase_09 AC 34 ms
53,460 KB
testcase_10 AC 35 ms
53,460 KB
testcase_11 AC 34 ms
53,460 KB
testcase_12 AC 34 ms
53,460 KB
testcase_13 AC 118 ms
77,860 KB
testcase_14 AC 114 ms
77,848 KB
testcase_15 AC 99 ms
76,864 KB
testcase_16 AC 116 ms
77,976 KB
testcase_17 AC 84 ms
76,608 KB
testcase_18 AC 108 ms
77,548 KB
testcase_19 AC 109 ms
77,844 KB
testcase_20 AC 94 ms
76,712 KB
testcase_21 AC 98 ms
76,980 KB
testcase_22 AC 107 ms
77,848 KB
testcase_23 AC 542 ms
135,564 KB
testcase_24 AC 496 ms
135,464 KB
testcase_25 AC 537 ms
135,340 KB
testcase_26 AC 514 ms
135,568 KB
testcase_27 AC 531 ms
135,828 KB
testcase_28 AC 497 ms
135,336 KB
testcase_29 AC 485 ms
135,088 KB
testcase_30 AC 513 ms
135,692 KB
testcase_31 AC 506 ms
135,212 KB
testcase_32 AC 542 ms
135,968 KB
testcase_33 AC 503 ms
134,204 KB
testcase_34 AC 529 ms
135,680 KB
testcase_35 AC 489 ms
133,704 KB
testcase_36 AC 498 ms
133,952 KB
testcase_37 AC 522 ms
134,204 KB
testcase_38 AC 520 ms
133,576 KB
testcase_39 AC 523 ms
133,448 KB
testcase_40 AC 504 ms
133,736 KB
testcase_41 AC 526 ms
133,576 KB
testcase_42 AC 511 ms
133,756 KB
testcase_43 AC 506 ms
133,576 KB
testcase_44 AC 528 ms
133,752 KB
testcase_45 AC 503 ms
133,576 KB
testcase_46 AC 516 ms
133,720 KB
testcase_47 AC 503 ms
133,576 KB
testcase_48 AC 568 ms
138,888 KB
testcase_49 AC 139 ms
82,368 KB
testcase_50 AC 360 ms
115,084 KB
testcase_51 AC 164 ms
90,188 KB
testcase_52 AC 232 ms
103,428 KB
testcase_53 AC 167 ms
112,944 KB
testcase_54 AC 149 ms
113,116 KB
testcase_55 AC 94 ms
84,736 KB
testcase_56 AC 216 ms
132,476 KB
testcase_57 AC 119 ms
94,824 KB
testcase_58 AC 400 ms
135,832 KB
testcase_59 AC 463 ms
135,344 KB
testcase_60 AC 570 ms
145,920 KB
testcase_61 AC 572 ms
139,280 KB
testcase_62 AC 540 ms
138,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

N=int(input())

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

for i in range(N-1):
    u,v=map(int,input().split())
    u-=1
    v-=1
    E[u].append(v)
    E[v].append(u)

A=list(map(int,input().split()))

# 木のHL分解+LCA

ROOT=0

QUE=[ROOT] 
Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート

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)

Children=[1]*N

ANS=0

for x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べる
    if x==ROOT:
        break
    Children[Parent[x]]+=Children[x]

for i in range(N):
    if Child[i]==[]:
        continue
    #if Parent[i]==N:
    #    continue
    LOW=[]
    HIGH=[]
    a=A[i]

    for c in Child[i]:
        if A[c]<a:
            LOW.append(Children[c])
        elif A[c]>a:
            HIGH.append(Children[c])

    p=Parent[i]
    if p!=N:
        if A[p]<a:
            LOW.append(N-Children[i])
        elif A[p]>a:
            HIGH.append(N-Children[i])

    #print(i,LOW,HIGH)

    S=sum(LOW)
    for l in LOW:
        S-=l
        ANS+=l*S

    S=sum(HIGH)
    for h in HIGH:
        S-=h
        ANS+=h*S

    ANS%=mod

print(ANS)
        

    
0