結果
問題 | No.2949 Product on Tree |
ユーザー | shimonohnishi |
提出日時 | 2024-11-04 10:38:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 665 ms / 2,000 ms |
コード長 | 1,406 bytes |
コンパイル時間 | 521 ms |
コンパイル使用メモリ | 82,392 KB |
実行使用メモリ | 140,224 KB |
最終ジャッジ日時 | 2024-11-04 10:39:08 |
合計ジャッジ時間 | 29,108 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
54,624 KB |
testcase_01 | AC | 41 ms
54,848 KB |
testcase_02 | AC | 42 ms
54,576 KB |
testcase_03 | AC | 634 ms
135,252 KB |
testcase_04 | AC | 563 ms
133,468 KB |
testcase_05 | AC | 616 ms
135,348 KB |
testcase_06 | AC | 583 ms
135,148 KB |
testcase_07 | AC | 596 ms
134,516 KB |
testcase_08 | AC | 583 ms
135,136 KB |
testcase_09 | AC | 601 ms
135,496 KB |
testcase_10 | AC | 573 ms
134,604 KB |
testcase_11 | AC | 584 ms
134,628 KB |
testcase_12 | AC | 594 ms
134,528 KB |
testcase_13 | AC | 561 ms
134,684 KB |
testcase_14 | AC | 585 ms
133,036 KB |
testcase_15 | AC | 560 ms
132,348 KB |
testcase_16 | AC | 571 ms
133,388 KB |
testcase_17 | AC | 549 ms
133,224 KB |
testcase_18 | AC | 577 ms
134,144 KB |
testcase_19 | AC | 561 ms
134,304 KB |
testcase_20 | AC | 589 ms
134,472 KB |
testcase_21 | AC | 591 ms
134,788 KB |
testcase_22 | AC | 571 ms
133,020 KB |
testcase_23 | AC | 665 ms
134,300 KB |
testcase_24 | AC | 632 ms
134,336 KB |
testcase_25 | AC | 614 ms
134,872 KB |
testcase_26 | AC | 628 ms
133,692 KB |
testcase_27 | AC | 585 ms
133,716 KB |
testcase_28 | AC | 617 ms
133,820 KB |
testcase_29 | AC | 591 ms
134,472 KB |
testcase_30 | AC | 572 ms
133,848 KB |
testcase_31 | AC | 563 ms
134,228 KB |
testcase_32 | AC | 576 ms
133,960 KB |
testcase_33 | AC | 553 ms
133,680 KB |
testcase_34 | AC | 599 ms
133,460 KB |
testcase_35 | AC | 576 ms
134,332 KB |
testcase_36 | AC | 595 ms
134,224 KB |
testcase_37 | AC | 565 ms
133,848 KB |
testcase_38 | AC | 602 ms
134,016 KB |
testcase_39 | AC | 565 ms
134,336 KB |
testcase_40 | AC | 567 ms
134,088 KB |
testcase_41 | AC | 580 ms
133,856 KB |
testcase_42 | AC | 562 ms
133,980 KB |
testcase_43 | AC | 213 ms
116,908 KB |
testcase_44 | AC | 207 ms
117,512 KB |
testcase_45 | AC | 250 ms
140,224 KB |
testcase_46 | AC | 228 ms
132,076 KB |
testcase_47 | AC | 186 ms
111,244 KB |
testcase_48 | AC | 239 ms
133,020 KB |
ソースコード
# 全方位木DP from collections import deque import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) mod = 998244353 X = [[] for i in range(N)] for i in range(N - 1): x, y = map(int, input().split()) X[x - 1].append(y - 1) X[y - 1].append(x - 1) P = [-1] * N Q = deque([0]) R = [] while Q: i = deque.popleft(Q) R.append(i) for a in X[i]: if a != P[i]: P[a] = i X[a].remove(i) deque.append(Q, a) # Settings unit = 0 def merge(a, b): return (a + b) % mod def adj_bu(a, i): return (a + 1) * A[i] % mod def adj_td(a, i, p): return (a + 1) * A[p] % mod def adj_fin(a, i): return (a + 1) * A[i] % mod ##### ME = [unit] * N XX = [0] * N TD = [unit] * N for i in R[1:][::-1]: XX[i] = adj_bu(ME[i], i) p = P[i] ME[p] = merge(ME[p], XX[i]) XX[R[0]] = adj_fin(ME[R[0]], R[0]) # print("ME =", ME) # Merge before adj # print("XX =", XX) # Bottom-up after adj for i in R: ac = TD[i] for j in X[i]: TD[j] = ac ac = merge(ac, XX[j]) ac = unit for j in X[i][::-1]: TD[j] = adj_td(merge(TD[j], ac), j, i) ac = merge(ac, XX[j]) XX[j] = adj_fin(merge(ME[j], TD[j]), j) # print("TD =", TD) # Top-down after adj # print("XX =", XX) # Final Result s = 0 for x in XX: s += x s %= mod s -= sum(A) s *= pow(2, mod - 2, mod) s %= mod print(s)