結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-11-30 00:00:53
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,363 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 166,268 KB
最終ジャッジ日時 2024-04-16 13:40:48
合計ジャッジ時間 25,262 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,760 KB
testcase_01 AC 46 ms
53,632 KB
testcase_02 AC 46 ms
53,888 KB
testcase_03 AC 1,085 ms
153,704 KB
testcase_04 AC 1,127 ms
158,112 KB
testcase_05 AC 1,059 ms
148,572 KB
testcase_06 AC 759 ms
126,100 KB
testcase_07 AC 295 ms
92,388 KB
testcase_08 AC 237 ms
87,292 KB
testcase_09 AC 349 ms
98,108 KB
testcase_10 AC 730 ms
123,980 KB
testcase_11 AC 426 ms
104,336 KB
testcase_12 AC 489 ms
106,552 KB
testcase_13 AC 1,169 ms
158,400 KB
testcase_14 AC 1,138 ms
158,060 KB
testcase_15 AC 1,125 ms
158,576 KB
testcase_16 AC 1,160 ms
157,628 KB
testcase_17 AC 1,156 ms
156,284 KB
testcase_18 AC 810 ms
131,840 KB
testcase_19 AC 898 ms
138,348 KB
testcase_20 AC 835 ms
133,208 KB
testcase_21 AC 640 ms
120,644 KB
testcase_22 AC 1,113 ms
154,876 KB
testcase_23 AC 48 ms
53,760 KB
testcase_24 AC 47 ms
54,016 KB
testcase_25 AC 47 ms
54,272 KB
testcase_26 AC 48 ms
53,760 KB
testcase_27 AC 47 ms
53,504 KB
testcase_28 RE -
testcase_29 AC 308 ms
97,152 KB
testcase_30 AC 808 ms
166,268 KB
testcase_31 AC 1,144 ms
164,884 KB
testcase_32 AC 1,135 ms
162,272 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 AC 853 ms
156,044 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