結果

問題 No.2214 Products on Tree
ユーザー chineristACchineristAC
提出日時 2023-02-10 21:47:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 805 ms / 3,000 ms
コード長 1,213 bytes
コンパイル時間 355 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 164,136 KB
最終ジャッジ日時 2023-09-21 22:56:42
合計ジャッジ時間 21,967 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 246 ms
92,828 KB
testcase_01 AC 242 ms
92,624 KB
testcase_02 AC 242 ms
92,568 KB
testcase_03 AC 768 ms
147,744 KB
testcase_04 AC 770 ms
150,204 KB
testcase_05 AC 695 ms
138,188 KB
testcase_06 AC 565 ms
126,516 KB
testcase_07 AC 338 ms
102,120 KB
testcase_08 AC 316 ms
101,684 KB
testcase_09 AC 367 ms
108,640 KB
testcase_10 AC 557 ms
122,816 KB
testcase_11 AC 410 ms
111,848 KB
testcase_12 AC 423 ms
116,280 KB
testcase_13 AC 761 ms
150,036 KB
testcase_14 AC 776 ms
149,584 KB
testcase_15 AC 760 ms
150,252 KB
testcase_16 AC 782 ms
150,216 KB
testcase_17 AC 805 ms
150,772 KB
testcase_18 AC 562 ms
138,636 KB
testcase_19 AC 599 ms
142,252 KB
testcase_20 AC 556 ms
136,908 KB
testcase_21 AC 445 ms
127,840 KB
testcase_22 AC 647 ms
158,780 KB
testcase_23 AC 250 ms
92,984 KB
testcase_24 AC 246 ms
92,860 KB
testcase_25 AC 254 ms
92,640 KB
testcase_26 AC 251 ms
93,028 KB
testcase_27 AC 252 ms
92,812 KB
testcase_28 AC 402 ms
137,620 KB
testcase_29 AC 362 ms
108,540 KB
testcase_30 AC 431 ms
148,764 KB
testcase_31 AC 668 ms
164,136 KB
testcase_32 AC 695 ms
156,036 KB
testcase_33 AC 447 ms
150,128 KB
testcase_34 AC 444 ms
150,112 KB
testcase_35 AC 614 ms
151,100 KB
testcase_36 AC 635 ms
149,652 KB
testcase_37 AC 451 ms
149,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd
from fractions import Fraction

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

mod = 998244353

N = int(input())
edge = [[] for v in range(N)]

for _ in range(N-1):
    a,b = mi()
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

parent = [-1] * N
topo = []
deq = deque([0])
while deq:
    v = deq.popleft()
    topo.append(v)
    for nv in edge[v]:
        if nv == parent[v]:
            continue
        parent[nv] = v
        deq.append(nv)

dp = [[0,0] for v in range(N)]
for v in topo[::-1]:
    dp[v] = [1,1]
    for nv in edge[v]:
        if nv == parent[v]:
            continue

        a,b = dp[v]
        c,d = dp[nv]

        ndp = [0,0]

        """
        つなげる場合
        """
        ndp[0] += a * c % mod
        ndp[1] += b * c % mod + a * d % mod

        """
        つなげない場合
        """
        ndp[0] += a * (d) % mod
        ndp[1] += b * (d) % mod

        ndp[0] %= mod
        ndp[1] %= mod

        dp[v] = ndp


print(dp[0][1])


0