結果

問題 No.2047 Path Factory
ユーザー sapphire__15sapphire__15
提出日時 2022-05-23 17:50:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 732 bytes
コンパイル時間 482 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 196,112 KB
最終ジャッジ日時 2023-10-20 17:39:24
合計ジャッジ時間 8,543 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,448 KB
testcase_01 AC 37 ms
53,448 KB
testcase_02 AC 37 ms
53,448 KB
testcase_03 AC 37 ms
53,448 KB
testcase_04 AC 36 ms
53,448 KB
testcase_05 AC 36 ms
53,448 KB
testcase_06 AC 37 ms
53,448 KB
testcase_07 AC 196 ms
83,516 KB
testcase_08 AC 177 ms
82,128 KB
testcase_09 AC 191 ms
83,164 KB
testcase_10 AC 243 ms
86,596 KB
testcase_11 AC 180 ms
82,112 KB
testcase_12 AC 152 ms
80,760 KB
testcase_13 AC 142 ms
80,204 KB
testcase_14 AC 180 ms
82,612 KB
testcase_15 AC 199 ms
83,656 KB
testcase_16 AC 239 ms
94,664 KB
testcase_17 AC 260 ms
97,856 KB
testcase_18 AC 226 ms
91,988 KB
testcase_19 AC 425 ms
131,384 KB
testcase_20 AC 468 ms
128,512 KB
testcase_21 AC 326 ms
117,180 KB
testcase_22 AC 128 ms
83,936 KB
testcase_23 AC 145 ms
81,108 KB
testcase_24 AC 133 ms
83,064 KB
testcase_25 AC 363 ms
186,500 KB
testcase_26 AC 560 ms
196,112 KB
testcase_27 AC 248 ms
88,684 KB
testcase_28 AC 153 ms
90,552 KB
testcase_29 AC 53 ms
63,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200000)
MOD = 998244353

N = int(input())
edge = [[] for _ in range(N)]
for i in range(N-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    edge[u].append(v)
    edge[v].append(u)

def dfs(now, prv = -1):
    global edge, dp
    # must, can cannot
    subDp = (1, 0, 0)
    for nxt in edge[now]:
        if nxt == prv: continue
        child = dfs(nxt, now)
        connect    = child[0] + child[1]
        disConnect = child[1] + child[2]
        subDp = (
            subDp[0] * disConnect % MOD,
            (subDp[0] * connect + subDp[1] * disConnect) % MOD,
            (subDp[1] * connect + subDp[2] * disConnect) % MOD
        )
    return subDp

print(sum(dfs(0)[1:]) % MOD)
0