結果

問題 No.2360 Path to Integer
ユーザー mkawa2mkawa2
提出日時 2023-06-23 23:04:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 311 ms / 2,500 ms
コード長 2,132 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 114,588 KB
最終ジャッジ日時 2024-07-01 02:51:36
合計ジャッジ時間 3,818 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,468 KB
testcase_01 AC 45 ms
54,632 KB
testcase_02 AC 41 ms
53,224 KB
testcase_03 AC 39 ms
52,984 KB
testcase_04 AC 40 ms
52,984 KB
testcase_05 AC 41 ms
54,044 KB
testcase_06 AC 40 ms
52,484 KB
testcase_07 AC 62 ms
67,256 KB
testcase_08 AC 102 ms
77,736 KB
testcase_09 AC 299 ms
106,164 KB
testcase_10 AC 247 ms
114,588 KB
testcase_11 AC 240 ms
114,104 KB
testcase_12 AC 178 ms
99,192 KB
testcase_13 AC 308 ms
106,284 KB
testcase_14 AC 273 ms
108,496 KB
testcase_15 AC 311 ms
106,084 KB
testcase_16 AC 268 ms
108,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# md = 10**9+7
md = 998244353

def rerooting(root=0):
    uu = []
    stack = [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        vv = []
        # !!!check the format of the "to"!!!
        for v in to[u]:
            if v == par[u]: continue
            vv.append(v)
            par[v] = u
            stack.append(v)
        to[u] = vv

    for u in uu[:0:-1]:
        p=par[u]
        cnt[p]+=cnt[u]

    # bottom up
    for u in uu[::-1]:
        p = par[u]
        if p == -1: continue
        dp[p] = merge(dp[p], trans_up(dp[u], u))

    # top down
    for u in uu:
        p = par[u]
        if p == -1: continue
        dp[u] = merge(dp[u], trans_down(purge(dp[p], trans_up(dp[u], u)), u))

# v is child
# a is parent's value
# b is child's value
def trans_up(valb, v):
    p = par[v]
    return (valb*pw[l[p]]+a[p]*cnt[v])%md

def trans_down(vala, v):
    p = par[v]
    c = n-cnt[v]
    return (vala*pw[l[v]]+a[v]*c)%md

def merge(a, b):
    return (a+b)%md

def purge(a, b):
    return (a-b)%md

pw = [1]
for _ in range(20): pw.append(pw[-1]*10%md)

n = II()
a = [0]*n
l = [0]*n
for i, s in enumerate(SI().split()):
    l[i] = len(s)
    a[i] = int(s)
to=[[] for _ in range(n)]
for _ in range(n-1):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)

cnt = [1]*n
par = [-1]*n
dp = a[:]

rerooting()

# print(cnt)
# print(dp)
ans=0
for d in dp:
    ans+=d
    ans%=md

print(ans)
0