結果

問題 No.2360 Path to Integer
ユーザー rlangevinrlangevin
提出日時 2024-05-03 17:37:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 639 ms / 2,500 ms
コード長 3,024 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 128,216 KB
最終ジャッジ日時 2024-05-03 17:37:51
合計ジャッジ時間 7,082 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,608 KB
testcase_01 AC 40 ms
52,608 KB
testcase_02 AC 38 ms
52,736 KB
testcase_03 AC 39 ms
52,864 KB
testcase_04 AC 38 ms
52,864 KB
testcase_05 AC 40 ms
52,992 KB
testcase_06 AC 41 ms
53,120 KB
testcase_07 AC 86 ms
76,544 KB
testcase_08 AC 170 ms
80,196 KB
testcase_09 AC 639 ms
120,964 KB
testcase_10 AC 444 ms
128,216 KB
testcase_11 AC 446 ms
127,516 KB
testcase_12 AC 392 ms
119,572 KB
testcase_13 AC 631 ms
120,616 KB
testcase_14 AC 575 ms
124,368 KB
testcase_15 AC 592 ms
121,852 KB
testcase_16 AC 568 ms
124,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0