結果

問題 No.2214 Products on Tree
ユーザー ShirotsumeShirotsume
提出日時 2022-12-22 01:50:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,048 ms / 3,000 ms
コード長 1,473 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 198,528 KB
最終ジャッジ日時 2024-04-29 03:36:55
合計ジャッジ時間 23,076 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,760 KB
testcase_01 AC 48 ms
54,016 KB
testcase_02 AC 46 ms
54,016 KB
testcase_03 AC 961 ms
181,628 KB
testcase_04 AC 1,025 ms
181,140 KB
testcase_05 AC 939 ms
175,420 KB
testcase_06 AC 653 ms
143,616 KB
testcase_07 AC 236 ms
95,744 KB
testcase_08 AC 178 ms
87,680 KB
testcase_09 AC 275 ms
103,040 KB
testcase_10 AC 625 ms
141,312 KB
testcase_11 AC 343 ms
111,360 KB
testcase_12 AC 401 ms
118,400 KB
testcase_13 AC 1,039 ms
182,108 KB
testcase_14 AC 1,048 ms
180,216 KB
testcase_15 AC 1,016 ms
182,448 KB
testcase_16 AC 1,037 ms
180,444 KB
testcase_17 AC 1,046 ms
180,132 KB
testcase_18 AC 604 ms
151,936 KB
testcase_19 AC 621 ms
155,520 KB
testcase_20 AC 581 ms
151,808 KB
testcase_21 AC 469 ms
131,328 KB
testcase_22 AC 791 ms
182,244 KB
testcase_23 AC 46 ms
53,632 KB
testcase_24 AC 46 ms
53,632 KB
testcase_25 AC 46 ms
53,504 KB
testcase_26 AC 46 ms
54,016 KB
testcase_27 AC 45 ms
54,016 KB
testcase_28 AC 359 ms
151,424 KB
testcase_29 AC 217 ms
101,248 KB
testcase_30 AC 467 ms
186,752 KB
testcase_31 AC 705 ms
186,556 KB
testcase_32 AC 734 ms
177,672 KB
testcase_33 AC 484 ms
198,528 KB
testcase_34 AC 497 ms
198,016 KB
testcase_35 AC 822 ms
180,968 KB
testcase_36 AC 823 ms
180,532 KB
testcase_37 AC 480 ms
178,924 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 CalcDepth(s, graph):
    INF = 2 ** 63 - 1
    from collections import deque
    n = len(graph)
    depth = [INF] * n
    depth[s] = 0
    q = deque()
    q.append(s)
    while q:
        now = q.popleft()
        for to in graph[now]:
            if depth[to] == INF:
                depth[to] = depth[now] + 1
                q.append(to)
    return depth

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)
    d = CalcDepth(0, graph)
    l = list(range(n))
    l.sort(key = lambda x: d[x], reverse=True)
    for now in l:
        dp[now][0] = 1
        dp[now][1] = 1
        s.add(now)
        for to in graph[now]:
            if to in s:
                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
    return dp[0][1]
        


n = ii()

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

print(solve(n, AB))


0