結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-11-27 18:40:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,481 ms / 3,000 ms
コード長 1,398 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 469,156 KB
最終ジャッジ日時 2024-04-16 13:37:34
合計ジャッジ時間 24,153 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,888 KB
testcase_01 AC 51 ms
54,528 KB
testcase_02 AC 50 ms
54,400 KB
testcase_03 AC 789 ms
162,896 KB
testcase_04 AC 836 ms
162,916 KB
testcase_05 AC 766 ms
156,828 KB
testcase_06 AC 564 ms
129,608 KB
testcase_07 AC 231 ms
91,904 KB
testcase_08 AC 200 ms
86,656 KB
testcase_09 AC 280 ms
98,036 KB
testcase_10 AC 539 ms
129,124 KB
testcase_11 AC 326 ms
104,192 KB
testcase_12 AC 368 ms
108,564 KB
testcase_13 AC 830 ms
162,228 KB
testcase_14 AC 816 ms
164,584 KB
testcase_15 AC 795 ms
160,660 KB
testcase_16 AC 819 ms
161,040 KB
testcase_17 AC 817 ms
161,624 KB
testcase_18 AC 568 ms
133,960 KB
testcase_19 AC 632 ms
143,584 KB
testcase_20 AC 570 ms
135,856 KB
testcase_21 AC 441 ms
122,616 KB
testcase_22 AC 768 ms
159,972 KB
testcase_23 AC 48 ms
53,888 KB
testcase_24 AC 48 ms
54,144 KB
testcase_25 AC 49 ms
54,272 KB
testcase_26 AC 48 ms
54,144 KB
testcase_27 AC 49 ms
54,016 KB
testcase_28 AC 964 ms
367,736 KB
testcase_29 AC 237 ms
98,816 KB
testcase_30 AC 492 ms
191,292 KB
testcase_31 AC 871 ms
190,748 KB
testcase_32 AC 845 ms
173,936 KB
testcase_33 AC 1,330 ms
469,144 KB
testcase_34 AC 1,333 ms
469,156 KB
testcase_35 AC 1,481 ms
346,620 KB
testcase_36 AC 1,458 ms
347,264 KB
testcase_37 AC 500 ms
161,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque, Counter
sys.setrecursionlimit(5 * 10 ** 5)
from pypyjit import set_param
set_param('max_unroll_recursion=-1')
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

def solve(n, AB):
    dp = [[0] * 2 for _ in range(n)]
    s = set()
    graph = [[] for _ in range(n)]
    for a, b in AB:
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)
    s.add(0)
    def dfs(now):
        dp[now][0] = dp[now][1] = 1
        des = []
        for to in graph[now]:
            if to not in s:
                des.append(to)
                s.add(to)
                dfs(to)

        for to in des:
            dp[now][0] *= (dp[to][0] + dp[to][1])
            dp[now][0] %= mod

        P = dp[now][0]
        dp[now][1] = P
        frprod = [1]
        backprod = [1]
        for to in des:
            frprod.append(frprod[-1] * (dp[to][0] + dp[to][1]) % mod)
        for to in des[::-1]:
            backprod.append(backprod[-1] * (dp[to][0] + dp[to][1]) % mod)

        for i, to in enumerate(des):
            dp[now][1] += (dp[to][1] * frprod[i] % mod) * backprod[len(des) - i - 1] % mod
            dp[now][1] %= mod
    dfs(0)
    return dp[0][1]
        


n = ii()

AB = [li() for _ in range(n - 1)]

print(solve(n, AB))


0