結果

問題 No.2360 Path to Integer
ユーザー 草苺奶昔草苺奶昔
提出日時 2024-05-03 02:53:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,233 ms / 2,500 ms
コード長 3,382 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 77,544 KB
最終ジャッジ日時 2024-11-23 23:22:24
合計ジャッジ時間 11,602 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
11,904 KB
testcase_01 AC 42 ms
11,776 KB
testcase_02 AC 41 ms
11,776 KB
testcase_03 AC 41 ms
11,776 KB
testcase_04 AC 40 ms
11,776 KB
testcase_05 AC 40 ms
11,904 KB
testcase_06 AC 42 ms
11,776 KB
testcase_07 AC 51 ms
12,544 KB
testcase_08 AC 137 ms
18,304 KB
testcase_09 AC 1,233 ms
75,316 KB
testcase_10 AC 1,104 ms
71,472 KB
testcase_11 AC 1,160 ms
77,544 KB
testcase_12 AC 960 ms
75,244 KB
testcase_13 AC 1,187 ms
70,080 KB
testcase_14 AC 1,196 ms
75,372 KB
testcase_15 AC 1,212 ms
75,216 KB
testcase_16 AC 1,180 ms
75,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import Callable, Generic, List, TypeVar

T = TypeVar("T")


class Rerooting(Generic[T]):

    __slots__ = ("adjList", "_n", "_decrement")

    def __init__(self, n: int, decrement: int = 0):
        self.adjList = [[] for _ in range(n)]
        self._n = n
        self._decrement = decrement

    def addEdge(self, u: int, v: int) -> None:
        u -= self._decrement
        v -= self._decrement
        self.adjList[u].append(v)
        self.adjList[v].append(u)

    def rerooting(
        self,
        e: Callable[[int], T],
        op: Callable[[T, T], T],
        composition: Callable[[T, int, int, int], T],
        root=0,
    ) -> List["T"]:
        root -= self._decrement
        assert 0 <= root < self._n
        parents = [-1] * self._n
        order = [root]
        stack = [root]
        while stack:
            cur = stack.pop()
            for next in self.adjList[cur]:
                if next == parents[cur]:
                    continue
                parents[next] = cur
                order.append(next)
                stack.append(next)

        dp1 = [e(i) for i in range(self._n)]
        dp2 = [e(i) for i in range(self._n)]
        for cur in order[::-1]:
            res = e(cur)
            for next in self.adjList[cur]:
                if parents[cur] == next:
                    continue
                dp2[next] = res
                res = op(res, composition(dp1[next], cur, next, 0))
            res = e(cur)
            for next in self.adjList[cur][::-1]:
                if parents[cur] == next:
                    continue
                dp2[next] = op(res, dp2[next])
                res = op(res, composition(dp1[next], cur, next, 0))
            dp1[cur] = res

        for newRoot in order[1:]:
            parent = parents[newRoot]
            dp2[newRoot] = composition(op(dp2[newRoot], dp2[parent]), parent, newRoot, 1)
            dp1[newRoot] = op(dp1[newRoot], dp2[newRoot])
        return dp1


import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")


from typing import Tuple

INF = int(4e18)
MOD = 998244353


pow10 = [1]
for _ in range(100):
    pow10.append(pow10[-1] * 10 % MOD)


if __name__ == "__main__":
    n = int(input())
    nums = [int(x) for x in input().split()]
    edges = []
    for _ in range(n - 1):
        u, v = map(int, input().split())
        u, v = u - 1, v - 1
        edges.append((u, v))
    pows = [pow10[len(str(nums[i]))] for i in range(n)]

    E = Tuple[int, int]  # (count,sum) 节点个数,路径和

    def e(root: int) -> E:
        return (0, 0)

    def op(childRes1: E, childRes2: E) -> E:
        c = childRes1[0] + childRes2[0]
        s = childRes1[1] + childRes2[1]
        if s >= MOD:
            s -= MOD
        return (c, s)

    def composition(fromRes: E, parent: int, child: int, direction: int) -> E:
        """direction: 0: child -> parent, 1: parent -> child"""
        c, s = fromRes
        from_ = child if direction == 0 else parent
        v, p = nums[from_], pows[from_]
        return (c + 1, (s * p + v * (c + 1)) % MOD)

    R = Rerooting(n)
    for u, v in edges:
        R.addEdge(u, v)
    dp = R.rerooting(e, op, composition)  # !注意dp不包含根节点
    res = 0
    for i in range(n):
        c, s = dp[i]
        v, p = nums[i], pows[i]
        res += s * p + v * (c + 1)
        res %= MOD
    print(res % MOD)
0