結果
問題 | No.2360 Path to Integer |
ユーザー | rlangevin |
提出日時 | 2024-05-03 17:37:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 499 ms / 2,500 ms |
コード長 | 3,024 bytes |
コンパイル時間 | 285 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 132,960 KB |
最終ジャッジ日時 | 2024-11-24 20:40:30 |
合計ジャッジ時間 | 5,584 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
53,020 KB |
testcase_01 | AC | 33 ms
53,704 KB |
testcase_02 | AC | 35 ms
54,376 KB |
testcase_03 | AC | 39 ms
54,636 KB |
testcase_04 | AC | 35 ms
53,408 KB |
testcase_05 | AC | 36 ms
53,540 KB |
testcase_06 | AC | 35 ms
53,108 KB |
testcase_07 | AC | 70 ms
76,804 KB |
testcase_08 | AC | 138 ms
80,196 KB |
testcase_09 | AC | 481 ms
121,304 KB |
testcase_10 | AC | 363 ms
127,972 KB |
testcase_11 | AC | 364 ms
132,960 KB |
testcase_12 | AC | 313 ms
119,756 KB |
testcase_13 | AC | 490 ms
121,208 KB |
testcase_14 | AC | 436 ms
124,388 KB |
testcase_15 | AC | 499 ms
121,540 KB |
testcase_16 | AC | 457 ms
124,848 KB |
ソースコード
import sys input = sys.stdin.readline mod = 998244353 class ReRooting(): def __init__(self, N): self.N = N self.G = [[] for i in range(N)] self.mem = [[] for i in range(N)] self.AcL = [None] * N self.AcR = [None] * N self.par = [-1] * N self.sz = [1] * N self.unit = 0 self.dp1 = [self.unit] * N self.dp2 = [self.unit] * N def add_edge(self, i, j): self.G[i].append(j) self.G[j].append(i) def merge(self, a, b): return (a + b) % mod def calc_dp(self, v, s, sz): return (v * pow(10, len(str(s)), mod) + s * sz) % mod def calc_ans(self, v, s): return (v * pow(10, len(str(s)), mod) + s * N) % mod def bottom_up(self, s): stack = [s] while stack: u = stack.pop() if u >= 0: stack.append(~u) for v in self.G[u]: if v == self.par[u]: continue self.par[v] = u stack.append(v) else: u = ~u self.mem[u] = [] for v in self.G[u]: if v == self.par[u]: continue self.sz[u] += self.sz[v] self.dp1[u] = self.merge(self.dp1[u], self.dp1[v]) self.mem[u].append(v) self.dp1[u] = self.calc_dp(self.dp1[u], A[u], self.sz[u]) Nt = len(self.mem[u]) self.AcL[u] = [self.unit] * (Nt + 1) self.AcR[u] = [self.unit] * (Nt + 1) for i, v in enumerate(self.mem[u]): self.AcL[u][i + 1] = self.merge(self.AcL[u][i], self.dp1[v]) for i, v in enumerate(self.mem[u][::-1]): self.AcR[u][i + 1] = self.merge(self.AcR[u][i], self.dp1[v]) return def top_down(self, s): stack = [s] while stack: u = stack.pop() for i, v in enumerate(self.mem[u]): self.dp2[v] = self.merge(self.merge(self.dp2[u], self.AcL[u][i]), self.AcR[u][len(self.mem[u]) - i - 1]) self.dp2[v] = self.calc_dp(self.dp2[v], A[u], N - self.sz[v]) stack.append(v) return def solve(self, s): self.bottom_up(s) self.top_down(s) # print(self.dp1) # print(self.dp2) ans = [None] * self.N for i in range(self.N): ans[i] = self.dp2[i] for u in self.G[i]: if self.par[i] == u: continue ans[i] = self.merge(ans[i], self.dp1[u]) ans[i] = self.calc_ans(ans[i], A[i]) return ans N = int(input()) A = list(map(int, input().split())) G = ReRooting(N) for _ in range(N - 1): u, v = map(int, input().split()) u, v = u - 1, v - 1 G.add_edge(u, v) print(sum(G.solve(0))%mod)