結果
問題 | No.2949 Product on Tree |
ユーザー | Shirotsume |
提出日時 | 2024-10-26 00:30:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,030 ms / 2,000 ms |
コード長 | 2,451 bytes |
コンパイル時間 | 346 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 132,116 KB |
最終ジャッジ日時 | 2024-10-26 00:31:25 |
合計ジャッジ時間 | 41,471 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
57,428 KB |
testcase_01 | AC | 44 ms
57,132 KB |
testcase_02 | AC | 44 ms
57,440 KB |
testcase_03 | AC | 766 ms
124,688 KB |
testcase_04 | AC | 722 ms
125,412 KB |
testcase_05 | AC | 837 ms
124,132 KB |
testcase_06 | AC | 876 ms
124,528 KB |
testcase_07 | AC | 819 ms
123,096 KB |
testcase_08 | AC | 948 ms
125,024 KB |
testcase_09 | AC | 886 ms
124,332 KB |
testcase_10 | AC | 848 ms
124,200 KB |
testcase_11 | AC | 899 ms
124,008 KB |
testcase_12 | AC | 896 ms
123,904 KB |
testcase_13 | AC | 886 ms
123,184 KB |
testcase_14 | AC | 857 ms
128,264 KB |
testcase_15 | AC | 856 ms
127,884 KB |
testcase_16 | AC | 921 ms
122,332 KB |
testcase_17 | AC | 843 ms
125,260 KB |
testcase_18 | AC | 866 ms
122,812 KB |
testcase_19 | AC | 905 ms
123,056 KB |
testcase_20 | AC | 935 ms
123,264 KB |
testcase_21 | AC | 889 ms
124,260 KB |
testcase_22 | AC | 948 ms
127,996 KB |
testcase_23 | AC | 924 ms
125,880 KB |
testcase_24 | AC | 892 ms
125,780 KB |
testcase_25 | AC | 850 ms
125,636 KB |
testcase_26 | AC | 880 ms
125,648 KB |
testcase_27 | AC | 947 ms
125,400 KB |
testcase_28 | AC | 880 ms
125,248 KB |
testcase_29 | AC | 896 ms
125,116 KB |
testcase_30 | AC | 892 ms
125,512 KB |
testcase_31 | AC | 933 ms
125,644 KB |
testcase_32 | AC | 1,003 ms
125,492 KB |
testcase_33 | AC | 908 ms
125,516 KB |
testcase_34 | AC | 988 ms
125,172 KB |
testcase_35 | AC | 952 ms
125,108 KB |
testcase_36 | AC | 942 ms
124,496 KB |
testcase_37 | AC | 1,030 ms
125,256 KB |
testcase_38 | AC | 920 ms
124,844 KB |
testcase_39 | AC | 947 ms
125,120 KB |
testcase_40 | AC | 963 ms
124,872 KB |
testcase_41 | AC | 949 ms
124,480 KB |
testcase_42 | AC | 902 ms
124,588 KB |
testcase_43 | AC | 235 ms
116,936 KB |
testcase_44 | AC | 221 ms
116,580 KB |
testcase_45 | AC | 264 ms
132,116 KB |
testcase_46 | AC | 244 ms
122,868 KB |
testcase_47 | AC | 203 ms
107,300 KB |
testcase_48 | AC | 271 ms
128,152 KB |
ソースコード
import sys, time, random from collections import deque, Counter, defaultdict def debug(*x):print('debug:',*x, file=sys.stderr) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 from collections import deque def TreeDepth(s, graph): inf = 2 ** 61 - 1 n = len(graph) depth = [inf] * n depth[s] = 0 q = deque() q.append(s) while q: now = q.popleft() for to in graph[now]: if depth[to] == inf: depth[to] = depth[now] + 1 q.append(to) return depth def TreeOrder(s, graph): dist = TreeDepth(s, graph) n = len(graph) l = list(range(n)) l.sort(key=lambda x: dist[x]) return l def subTree(s, graph): l = TreeOrder(s, graph) n = len(graph) sub = [0] * n for v in l[::-1]: sub[v] = 1 for to in graph[v]: sub[v] += sub[to] return sub def Treeheight(s, graph): l = TreeOrder(s, graph) n = len(graph) height = [0] * n for v in l[::-1]: height[v] = max([height[to] for to in graph[v]] + [0]) + 1 return height def EulerTour(s, graph): n = len(graph) done = [0] * n Q = [~s, s] # 根をスタックに追加 ET = [] while Q: i = Q.pop() if i >= 0: # 行きがけの処理 done[i] = 1 ET.append(i) for a in graph[i][::-1]: if done[a]: continue Q.append(~a) # 帰りがけの処理をスタックに追加 Q.append(a) # 行きがけの処理をスタックに追加 else: # 帰りがけの処理 ET.append(~i) return ET n = ii() a = li() graph = [[] for _ in range(n)] for _ in range(n - 1): u, v = mi() u -= 1 v -= 1 graph[u].append(v) graph[v].append(u) L = TreeOrder(0, graph) d = TreeDepth(0, graph) dp = [1] * n ans = 0 i2 = pow(2, -1, mod) for v in L[::-1]: s = 0 nans = 0 dp[v] = a[v] f = 0 for to in graph[v]: if d[to] > d[v]: f = 1 dp[v] += a[v] * dp[to] nans -= a[v] * dp[to] % mod * dp[to] dp[v] %= mod nans %= mod s += dp[to] nans += a[v] * s * s nans *= i2 nans %= mod ans += dp[v] + nans - a[v] ans %= mod if not f: dp[v] = a[v] print(ans)