結果

問題 No.2047 Path Factory
ユーザー asumo0729asumo0729
提出日時 2022-09-18 22:10:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 463 ms / 2,000 ms
コード長 1,715 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 109,696 KB
最終ジャッジ日時 2023-08-23 18:20:58
合計ジャッジ時間 9,222 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
72,596 KB
testcase_01 AC 113 ms
72,692 KB
testcase_02 AC 110 ms
72,300 KB
testcase_03 AC 109 ms
72,700 KB
testcase_04 AC 110 ms
72,564 KB
testcase_05 AC 110 ms
72,656 KB
testcase_06 AC 111 ms
72,696 KB
testcase_07 AC 307 ms
93,012 KB
testcase_08 AC 269 ms
90,112 KB
testcase_09 AC 294 ms
92,588 KB
testcase_10 AC 363 ms
97,624 KB
testcase_11 AC 275 ms
90,688 KB
testcase_12 AC 217 ms
85,776 KB
testcase_13 AC 229 ms
86,228 KB
testcase_14 AC 280 ms
91,312 KB
testcase_15 AC 306 ms
94,008 KB
testcase_16 AC 283 ms
89,424 KB
testcase_17 AC 280 ms
89,296 KB
testcase_18 AC 300 ms
91,232 KB
testcase_19 AC 327 ms
93,412 KB
testcase_20 AC 410 ms
99,444 KB
testcase_21 AC 300 ms
90,720 KB
testcase_22 AC 171 ms
82,100 KB
testcase_23 AC 202 ms
82,620 KB
testcase_24 AC 197 ms
82,676 KB
testcase_25 AC 259 ms
101,960 KB
testcase_26 AC 463 ms
104,028 KB
testcase_27 AC 418 ms
104,996 KB
testcase_28 AC 258 ms
109,696 KB
testcase_29 AC 128 ms
77,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import itemgetter
from collections import defaultdict, deque
import heapq
from heapq import heapify, heappop, _heapify_max, heappush
from bisect import bisect_left, bisect_right
import math
import itertools
import copy

stdin=sys.stdin
#sys.setrecursionlimit(10 ** 7)
## import pypyjit
## pypyjit.set_param('max_unroll_recursion=-1')

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
Yp=lambda:print('Yes')
Np=lambda:print('No')
inf = 1 << 60
mod = 10 ** 9 + 7
mod = 998244353
eps = 1e-9
sortkey1 = itemgetter(0)
sortkey2 = lambda x: (x[0], x[1])



###############################################################

N = ip()
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = lp()
    u -= 1; v -= 1
    adj[u].append(v)
    adj[v].append(u)
dp = [[0 for _ in range(3)] for _ in range(N)]
visit = [False for _ in range(N)]
start = 0
deq = deque([(~start, -1), (start, -1)])
while deq:
    now, par = deq.pop()
    ## 行きがけ
    if now >= 0:
        visit[now] = True
        for nex in adj[now]:
            if visit[nex]:
                continue
            deq.append((~nex, now))
            deq.append((nex, now))
    ## 帰りがけ
    else:
        x0 = 1; x1 = 0; x2 = 0
        for ch in adj[~now]:
            if ch != par:
                x2 = (x2 * (dp[ch][1] + dp[ch][2]) + x1 * (dp[ch][0] + dp[ch][1])) % mod
                x1 = (x0 * (dp[ch][0] + dp[ch][1]) + x1 * (dp[ch][1] + dp[ch][2])) % mod
                x0 = x0 * (dp[ch][1] + dp[ch][2]) % mod
        dp[~now][0] = x0
        dp[~now][1] = x1
        dp[~now][2] = x2

ans = dp[0][1] + dp[0][2]
ans %= mod
print(ans)
0