結果
問題 | No.2949 Product on Tree |
ユーザー | detteiuu |
提出日時 | 2024-10-25 22:30:26 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,986 ms / 2,000 ms |
コード長 | 1,907 bytes |
コンパイル時間 | 240 ms |
コンパイル使用メモリ | 82,400 KB |
実行使用メモリ | 279,788 KB |
最終ジャッジ日時 | 2024-10-25 22:31:39 |
合計ジャッジ時間 | 68,795 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,356 KB |
testcase_01 | AC | 38 ms
54,012 KB |
testcase_02 | AC | 40 ms
54,388 KB |
testcase_03 | AC | 1,283 ms
165,032 KB |
testcase_04 | AC | 1,235 ms
163,180 KB |
testcase_05 | AC | 1,271 ms
165,428 KB |
testcase_06 | AC | 1,251 ms
165,964 KB |
testcase_07 | AC | 1,346 ms
164,648 KB |
testcase_08 | AC | 1,336 ms
167,136 KB |
testcase_09 | AC | 1,315 ms
169,412 KB |
testcase_10 | AC | 1,282 ms
170,668 KB |
testcase_11 | AC | 1,332 ms
179,372 KB |
testcase_12 | AC | 1,418 ms
194,308 KB |
testcase_13 | AC | 1,467 ms
216,420 KB |
testcase_14 | AC | 1,611 ms
239,764 KB |
testcase_15 | AC | 1,905 ms
258,168 KB |
testcase_16 | AC | 1,685 ms
262,484 KB |
testcase_17 | AC | 1,664 ms
262,312 KB |
testcase_18 | AC | 1,720 ms
263,004 KB |
testcase_19 | AC | 1,662 ms
264,332 KB |
testcase_20 | AC | 1,844 ms
263,940 KB |
testcase_21 | AC | 1,772 ms
270,692 KB |
testcase_22 | AC | 1,624 ms
259,148 KB |
testcase_23 | AC | 1,322 ms
166,780 KB |
testcase_24 | AC | 1,353 ms
167,300 KB |
testcase_25 | AC | 1,335 ms
167,820 KB |
testcase_26 | AC | 1,393 ms
168,352 KB |
testcase_27 | AC | 1,399 ms
168,208 KB |
testcase_28 | AC | 1,375 ms
168,452 KB |
testcase_29 | AC | 1,388 ms
171,004 KB |
testcase_30 | AC | 1,405 ms
174,452 KB |
testcase_31 | AC | 1,410 ms
180,476 KB |
testcase_32 | AC | 1,503 ms
198,328 KB |
testcase_33 | AC | 1,683 ms
220,148 KB |
testcase_34 | AC | 1,735 ms
249,504 KB |
testcase_35 | AC | 1,847 ms
267,400 KB |
testcase_36 | AC | 1,916 ms
272,624 KB |
testcase_37 | AC | 1,972 ms
272,388 KB |
testcase_38 | AC | 1,986 ms
276,872 KB |
testcase_39 | AC | 1,825 ms
271,880 KB |
testcase_40 | AC | 1,916 ms
276,972 KB |
testcase_41 | AC | 1,907 ms
274,796 KB |
testcase_42 | AC | 1,842 ms
279,788 KB |
testcase_43 | AC | 399 ms
138,848 KB |
testcase_44 | AC | 409 ms
140,444 KB |
testcase_45 | AC | 495 ms
169,632 KB |
testcase_46 | AC | 486 ms
151,292 KB |
testcase_47 | AC | 368 ms
130,276 KB |
testcase_48 | AC | 469 ms
153,028 KB |
ソースコード
from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc N = int(input()) A = list(map(int, input().split())) G = [[] for _ in range(N)] for _ in range(N-1): u, v = map(int, input().split()) G[u-1].append(v-1) G[v-1].append(u-1) MOD = 998244353 @bootstrap def dfs(n, pre): ans = 0 for i, v in enumerate(G[n]): if v != pre: dp[n][i] = yield dfs(v, n) ans += dp[n][i] ans %= MOD yield ((ans*A[n])%MOD+A[n])%MOD dp = [[0]*len(G[i]) for i in range(N)] dfs(0, -1) def LRset(n): for i in range(1, len(G[n])): L[n][i] = (L[n][i-1]+dp[n][i-1])%MOD for i in reversed(range(len(G[n])-1)): R[n][i] = (R[n][i+1]+dp[n][i+1])%MOD L = [[0]*len(G[i]) for i in range(N)] R = [[0]*len(G[i]) for i in range(N)] LRset(0) @bootstrap def rerooting(n, pre, no): visited[n] = True preno = 0 for i in range(len(G[n])): if G[n][i] == pre: preno = i break dp[n][preno] = ((L[pre][no]+R[pre][no])*A[pre]%MOD+A[pre])%MOD LRset(n) for i in range(len(G[n])): if not visited[G[n][i]]: yield rerooting(G[n][i], n, i) yield visited = [False]*N visited[0] = True for i in range(len(G[0])): if not visited[G[0][i]]: rerooting(G[0][i], 0, i) ans = 0 for i, d in enumerate(dp): for n in d: ans += n*A[i] ans %= MOD def inverse(n, d): return n * pow(d, -1, MOD) % MOD print(inverse(ans, 2))