結果

問題 No.2360 Path to Integer
ユーザー mkawa2mkawa2
提出日時 2023-06-23 23:04:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 312 ms / 2,500 ms
コード長 2,132 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 86,632 KB
実行使用メモリ 117,916 KB
最終ジャッジ日時 2023-09-13 18:13:23
合計ジャッジ時間 4,414 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,188 KB
testcase_01 AC 72 ms
71,396 KB
testcase_02 AC 72 ms
71,192 KB
testcase_03 AC 72 ms
71,400 KB
testcase_04 AC 71 ms
71,404 KB
testcase_05 AC 72 ms
71,396 KB
testcase_06 AC 72 ms
71,092 KB
testcase_07 AC 93 ms
76,412 KB
testcase_08 AC 126 ms
78,888 KB
testcase_09 AC 309 ms
102,544 KB
testcase_10 AC 270 ms
117,916 KB
testcase_11 AC 271 ms
117,908 KB
testcase_12 AC 212 ms
106,384 KB
testcase_13 AC 305 ms
102,432 KB
testcase_14 AC 279 ms
106,164 KB
testcase_15 AC 312 ms
103,908 KB
testcase_16 AC 282 ms
107,760 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