結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-11-25 04:53:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,590 ms / 3,000 ms
コード長 1,336 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 444,400 KB
最終ジャッジ日時 2024-04-16 13:39:14
合計ジャッジ時間 30,812 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
53,632 KB
testcase_01 AC 49 ms
53,888 KB
testcase_02 AC 48 ms
53,760 KB
testcase_03 AC 1,109 ms
158,104 KB
testcase_04 AC 1,141 ms
158,792 KB
testcase_05 AC 1,072 ms
151,808 KB
testcase_06 AC 753 ms
128,768 KB
testcase_07 AC 297 ms
92,032 KB
testcase_08 AC 239 ms
86,816 KB
testcase_09 AC 353 ms
98,320 KB
testcase_10 AC 726 ms
126,772 KB
testcase_11 AC 430 ms
104,576 KB
testcase_12 AC 491 ms
108,416 KB
testcase_13 AC 1,216 ms
162,160 KB
testcase_14 AC 1,173 ms
160,284 KB
testcase_15 AC 1,154 ms
160,800 KB
testcase_16 AC 1,168 ms
159,236 KB
testcase_17 AC 1,168 ms
156,964 KB
testcase_18 AC 817 ms
133,832 KB
testcase_19 AC 906 ms
140,240 KB
testcase_20 AC 821 ms
133,632 KB
testcase_21 AC 645 ms
120,448 KB
testcase_22 AC 1,137 ms
157,456 KB
testcase_23 AC 47 ms
53,888 KB
testcase_24 AC 47 ms
53,888 KB
testcase_25 AC 46 ms
54,144 KB
testcase_26 AC 47 ms
54,400 KB
testcase_27 AC 47 ms
53,760 KB
testcase_28 AC 1,134 ms
324,164 KB
testcase_29 AC 315 ms
97,536 KB
testcase_30 AC 809 ms
164,480 KB
testcase_31 AC 1,217 ms
166,816 KB
testcase_32 AC 1,160 ms
163,116 KB
testcase_33 AC 1,576 ms
444,008 KB
testcase_34 AC 1,583 ms
444,400 KB
testcase_35 AC 1,576 ms
305,816 KB
testcase_36 AC 1,590 ms
305,536 KB
testcase_37 AC 867 ms
157,464 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
        for to in des:
            P *= pow(dp[to][0] + dp[to][1], mod - 2, mod)
            P %= mod
            P *= dp[to][1]
            P %= mod
            dp[now][1] += P
            dp[now][1] %= mod
            P *= pow(dp[to][1], mod - 2, mod)
            P %= mod
            P *= (dp[to][0] + dp[to][1])
            P %= mod
    dfs(0)
    return dp[0][1]
        


n = ii()

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

print(solve(n, AB))


0