結果

問題 No.2047 Path Factory
ユーザー chineristACchineristAC
提出日時 2022-08-19 22:23:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 376 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 106,916 KB
最終ジャッジ日時 2024-04-23 13:31:10
合計ジャッジ時間 4,739 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,936 KB
testcase_01 AC 42 ms
55,808 KB
testcase_02 AC 47 ms
56,064 KB
testcase_03 AC 44 ms
55,936 KB
testcase_04 AC 42 ms
55,936 KB
testcase_05 AC 43 ms
55,680 KB
testcase_06 AC 42 ms
56,088 KB
testcase_07 AC 158 ms
94,892 KB
testcase_08 AC 135 ms
86,912 KB
testcase_09 AC 153 ms
94,220 KB
testcase_10 AC 205 ms
102,096 KB
testcase_11 AC 141 ms
92,160 KB
testcase_12 AC 112 ms
85,504 KB
testcase_13 AC 118 ms
85,724 KB
testcase_14 AC 147 ms
92,800 KB
testcase_15 AC 164 ms
95,232 KB
testcase_16 AC 125 ms
85,504 KB
testcase_17 AC 119 ms
85,036 KB
testcase_18 AC 152 ms
92,672 KB
testcase_19 AC 155 ms
94,360 KB
testcase_20 AC 214 ms
103,704 KB
testcase_21 AC 155 ms
92,356 KB
testcase_22 AC 87 ms
78,848 KB
testcase_23 AC 91 ms
79,104 KB
testcase_24 AC 95 ms
78,924 KB
testcase_25 AC 139 ms
105,472 KB
testcase_26 AC 220 ms
102,908 KB
testcase_27 AC 221 ms
102,400 KB
testcase_28 AC 146 ms
106,916 KB
testcase_29 AC 52 ms
66,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush
from itertools import cycle, permutations
from math import log,gcd

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):
    u,v = mi()
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)

parent = [-1] * N
deq = deque([0])
topo = []
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,0] for v in range(N)]

"""
0:根が1点のみ
1:根がパスの端点
2:根がパスの中
"""

for v in topo[::-1]:
    a,b,c = 1,0,0
    for nv in edge[v]:
        if nv == parent[v]:
            continue

        c = (b * (dp[nv][1] + dp[nv][0]) + c * (dp[nv][1] + dp[nv][2])) % mod
        b = (a * (dp[nv][1]+dp[nv][0]) + b * (dp[nv][1] + dp[nv][2])) % mod
        a = a * (dp[nv][1] + dp[nv][2]) % mod
    
    dp[v] = [a,b,c]

res = dp[0][1] + dp[0][2]

print(res % mod)


0