結果

問題 No.2360 Path to Integer
ユーザー mkawa2
提出日時 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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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