結果

問題 No.2598 Kadomatsu on Tree
ユーザー rlangevinrlangevin
提出日時 2024-01-05 12:23:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 139,140 KB
最終ジャッジ日時 2024-01-05 12:24:29
合計ジャッジ時間 21,137 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,460 KB
testcase_01 AC 45 ms
53,460 KB
testcase_02 AC 40 ms
53,460 KB
testcase_03 AC 39 ms
53,460 KB
testcase_04 AC 39 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 39 ms
53,460 KB
testcase_07 AC 41 ms
53,460 KB
testcase_08 AC 41 ms
53,460 KB
testcase_09 AC 41 ms
53,460 KB
testcase_10 AC 40 ms
53,460 KB
testcase_11 AC 39 ms
53,460 KB
testcase_12 AC 38 ms
53,460 KB
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 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 188 ms
106,264 KB
testcase_54 AC 180 ms
99,804 KB
testcase_55 AC 106 ms
82,692 KB
testcase_56 AC 252 ms
121,368 KB
testcase_57 AC 130 ms
92,324 KB
testcase_58 AC 369 ms
132,892 KB
testcase_59 AC 365 ms
133,044 KB
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[u].append(v)
    G[v].append(u)
    
A = list(map(int, input().split()))
mod = 998244353
    
def non_rec_dfs(s):
    stack = []
    stack.append(s)
    par = [-1] * N
    seen = [0] * N
    sz = [1] * N
    ans = 0
    while stack:
        u = stack.pop()
        seen[u] = 1
        if u >= 0:
            stack.append(~u)
            for v in G[u]:
                if v == par[u] or seen[v]:
                    continue
                par[v] = u
                stack.append(v)
        else:
            u = ~u
            for v in G[u]:
                if v == par[u]:
                    continue
                sz[u] += sz[v]
                
            p2, p1, m2, m1 = 0, 0, 0, 0
            for v in G[u]:
                val = sz[v]
                if v == par[u]:
                    val = N - sz[u]
                if A[v] > A[u]:
                    p2 += val ** 2
                    p1 += val
                elif A[v] < A[u]:
                    m2 += val ** 2
                    m1 += val
            ans += (p1**2 - p2)//2 + (m1**2 - m2)//2
            ans %= mod

    return ans

print(non_rec_dfs(0))
0