結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-12-05 02:46:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,122 ms / 3,000 ms
コード長 1,072 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 82,668 KB
実行使用メモリ 345,804 KB
最終ジャッジ日時 2024-04-20 07:09:19
合計ジャッジ時間 20,815 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,452 KB
testcase_01 AC 46 ms
55,396 KB
testcase_02 AC 48 ms
55,008 KB
testcase_03 AC 721 ms
161,716 KB
testcase_04 AC 742 ms
160,336 KB
testcase_05 AC 907 ms
159,944 KB
testcase_06 AC 472 ms
130,776 KB
testcase_07 AC 175 ms
92,840 KB
testcase_08 AC 142 ms
87,040 KB
testcase_09 AC 217 ms
97,464 KB
testcase_10 AC 480 ms
130,576 KB
testcase_11 AC 262 ms
104,932 KB
testcase_12 AC 394 ms
110,564 KB
testcase_13 AC 744 ms
164,184 KB
testcase_14 AC 714 ms
164,964 KB
testcase_15 AC 740 ms
164,548 KB
testcase_16 AC 744 ms
164,308 KB
testcase_17 AC 750 ms
163,692 KB
testcase_18 AC 431 ms
133,384 KB
testcase_19 AC 477 ms
139,036 KB
testcase_20 AC 430 ms
131,644 KB
testcase_21 AC 343 ms
119,732 KB
testcase_22 AC 583 ms
155,740 KB
testcase_23 AC 42 ms
54,200 KB
testcase_24 AC 42 ms
54,340 KB
testcase_25 AC 42 ms
55,584 KB
testcase_26 AC 42 ms
54,728 KB
testcase_27 AC 43 ms
53,668 KB
testcase_28 AC 706 ms
271,700 KB
testcase_29 AC 176 ms
96,552 KB
testcase_30 AC 389 ms
159,716 KB
testcase_31 AC 600 ms
157,292 KB
testcase_32 AC 606 ms
157,456 KB
testcase_33 AC 966 ms
345,804 KB
testcase_34 AC 973 ms
344,832 KB
testcase_35 AC 1,122 ms
258,308 KB
testcase_36 AC 1,115 ms
260,008 KB
testcase_37 AC 456 ms
163,304 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] = 1
        dp[now][1] = 1
        for to in graph[now]:
            if to not in s:
                s.add(to)
                dfs(to)
                nc = [0, 0]
                nc[0] = dp[now][0] * (dp[to][0] + dp[to][1])
                nc[0] %= mod
                nc[1] = dp[now][0] * dp[to][1] + dp[now][1] * (dp[to][0] + dp[to][1])
                nc[1] %= mod
                dp[now] = nc
    dfs(0)
    return dp[0][1]
        


n = ii()

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

print(solve(n, AB))


0