結果

問題 No.2598 Kadomatsu on Tree
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-12-18 19:43:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 906 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 175,288 KB
最終ジャッジ日時 2024-01-01 08:30:45
合計ジャッジ時間 35,072 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,476 KB
testcase_01 AC 39 ms
55,476 KB
testcase_02 AC 38 ms
55,476 KB
testcase_03 AC 39 ms
55,476 KB
testcase_04 AC 39 ms
55,476 KB
testcase_05 AC 39 ms
55,476 KB
testcase_06 AC 39 ms
55,476 KB
testcase_07 AC 39 ms
55,476 KB
testcase_08 AC 39 ms
55,476 KB
testcase_09 AC 39 ms
55,476 KB
testcase_10 AC 39 ms
55,476 KB
testcase_11 AC 38 ms
55,476 KB
testcase_12 AC 38 ms
55,476 KB
testcase_13 AC 131 ms
78,464 KB
testcase_14 AC 129 ms
78,592 KB
testcase_15 AC 121 ms
78,208 KB
testcase_16 AC 123 ms
78,720 KB
testcase_17 AC 112 ms
77,960 KB
testcase_18 AC 126 ms
78,464 KB
testcase_19 AC 125 ms
78,592 KB
testcase_20 AC 119 ms
78,096 KB
testcase_21 AC 122 ms
78,464 KB
testcase_22 AC 119 ms
78,592 KB
testcase_23 AC 906 ms
170,152 KB
testcase_24 AC 790 ms
174,204 KB
testcase_25 AC 795 ms
174,368 KB
testcase_26 AC 800 ms
170,980 KB
testcase_27 AC 803 ms
171,244 KB
testcase_28 AC 771 ms
173,740 KB
testcase_29 AC 782 ms
175,288 KB
testcase_30 AC 768 ms
169,964 KB
testcase_31 AC 771 ms
174,868 KB
testcase_32 AC 767 ms
172,276 KB
testcase_33 AC 777 ms
175,208 KB
testcase_34 AC 742 ms
168,396 KB
testcase_35 AC 792 ms
169,908 KB
testcase_36 AC 798 ms
170,284 KB
testcase_37 AC 789 ms
174,764 KB
testcase_38 AC 776 ms
170,036 KB
testcase_39 AC 753 ms
170,036 KB
testcase_40 AC 766 ms
170,036 KB
testcase_41 AC 770 ms
170,036 KB
testcase_42 AC 793 ms
170,036 KB
testcase_43 AC 782 ms
169,652 KB
testcase_44 AC 769 ms
170,036 KB
testcase_45 AC 762 ms
169,652 KB
testcase_46 AC 756 ms
170,036 KB
testcase_47 AC 740 ms
169,780 KB
testcase_48 AC 706 ms
164,276 KB
testcase_49 AC 145 ms
83,472 KB
testcase_50 AC 470 ms
140,980 KB
testcase_51 AC 193 ms
92,940 KB
testcase_52 AC 264 ms
104,368 KB
testcase_53 AC 279 ms
122,296 KB
testcase_54 AC 256 ms
111,360 KB
testcase_55 AC 166 ms
89,688 KB
testcase_56 AC 389 ms
149,160 KB
testcase_57 AC 199 ms
102,584 KB
testcase_58 AC 671 ms
171,900 KB
testcase_59 AC 692 ms
174,200 KB
testcase_60 AC 724 ms
159,860 KB
testcase_61 AC 757 ms
169,516 KB
testcase_62 AC 745 ms
160,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD=998244353
def pairs(arr):
    add=0
    squ=0
    for i in arr:
        add+=i
        squ+=i*i
    return (add*add-squ)//2
N=int(input())
tree=[set() for i in range(N)]
for i in range(N-1):
    U,V=map(int,input().split())
    tree[U-1].add(V-1)
    tree[V-1].add(U-1)
A=list(map(int,input().split()))
top=[]
vert=deque([0])
dp=[-1]*N
while len(vert)>0:
    pos=vert.popleft()
    top.append(pos)
    dp[pos]=0
    for i in tree[pos]:
        if dp[i]==-1:
            vert.append(i)
for i in reversed(top):
    for j in tree[i]:
        dp[i]+=dp[j]
    dp[i]+=1
ans=0
for i in range(N):
    rem=-1
    less=[]
    gre=[]
    for j in tree[i]:
        if dp[i]<dp[j]:
            rem=j
            continue
        if A[i]<A[j]:
            less.append(dp[j])
        elif A[i]>A[j]:
            gre.append(dp[j])
    if rem!=-1:
        if A[i]<A[rem]:
            less.append(N-dp[i])
        elif A[i]>A[rem]:
            gre.append(N-dp[i])
    ans+=pairs(less)+pairs(gre)
    ans%=MOD
print(ans)
0