結果
問題 | No.2949 Product on Tree |
ユーザー | tassei903 |
提出日時 | 2024-10-25 21:55:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 653 ms / 2,000 ms |
コード長 | 1,680 bytes |
コンパイル時間 | 290 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 140,696 KB |
最終ジャッジ日時 | 2024-10-25 21:56:24 |
合計ジャッジ時間 | 29,025 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
54,048 KB |
testcase_01 | AC | 44 ms
54,460 KB |
testcase_02 | AC | 43 ms
54,132 KB |
testcase_03 | AC | 624 ms
134,972 KB |
testcase_04 | AC | 627 ms
133,560 KB |
testcase_05 | AC | 615 ms
135,276 KB |
testcase_06 | AC | 621 ms
135,016 KB |
testcase_07 | AC | 582 ms
133,880 KB |
testcase_08 | AC | 624 ms
135,148 KB |
testcase_09 | AC | 570 ms
135,136 KB |
testcase_10 | AC | 612 ms
134,460 KB |
testcase_11 | AC | 639 ms
134,740 KB |
testcase_12 | AC | 570 ms
134,444 KB |
testcase_13 | AC | 572 ms
134,340 KB |
testcase_14 | AC | 595 ms
132,988 KB |
testcase_15 | AC | 575 ms
132,484 KB |
testcase_16 | AC | 596 ms
133,416 KB |
testcase_17 | AC | 608 ms
133,236 KB |
testcase_18 | AC | 570 ms
134,528 KB |
testcase_19 | AC | 557 ms
134,152 KB |
testcase_20 | AC | 609 ms
134,980 KB |
testcase_21 | AC | 594 ms
134,884 KB |
testcase_22 | AC | 569 ms
132,916 KB |
testcase_23 | AC | 641 ms
134,116 KB |
testcase_24 | AC | 653 ms
134,644 KB |
testcase_25 | AC | 621 ms
134,356 KB |
testcase_26 | AC | 624 ms
133,876 KB |
testcase_27 | AC | 629 ms
133,596 KB |
testcase_28 | AC | 620 ms
133,856 KB |
testcase_29 | AC | 622 ms
134,364 KB |
testcase_30 | AC | 634 ms
134,000 KB |
testcase_31 | AC | 602 ms
134,384 KB |
testcase_32 | AC | 592 ms
133,480 KB |
testcase_33 | AC | 620 ms
133,752 KB |
testcase_34 | AC | 599 ms
133,712 KB |
testcase_35 | AC | 574 ms
134,224 KB |
testcase_36 | AC | 622 ms
133,868 KB |
testcase_37 | AC | 593 ms
133,868 KB |
testcase_38 | AC | 594 ms
133,864 KB |
testcase_39 | AC | 596 ms
134,016 KB |
testcase_40 | AC | 613 ms
133,988 KB |
testcase_41 | AC | 582 ms
133,996 KB |
testcase_42 | AC | 623 ms
133,848 KB |
testcase_43 | AC | 224 ms
116,684 KB |
testcase_44 | AC | 210 ms
117,008 KB |
testcase_45 | AC | 253 ms
140,696 KB |
testcase_46 | AC | 231 ms
132,316 KB |
testcase_47 | AC | 188 ms
110,824 KB |
testcase_48 | AC | 232 ms
132,716 KB |
ソースコード
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### # 全方位木DP import sys input = sys.stdin.readline from collections import deque mod = 998244353 N = ni() A = na() 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 merge = lambda a, b: (a + b) % mod adj_bu = lambda a, i: (a + 1) * A[i] % mod adj_td = lambda a, i, p: (a + 1) * A[p] % mod adj_fin = lambda a, i: (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 # print(sum(XX)-sum(A)) print((sum(XX)-sum(A)) * pow(2, mod-2, mod)%mod)