結果

問題 No.2047 Path Factory
ユーザー sapphire__15sapphire__15
提出日時 2022-05-23 23:28:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 571 ms / 2,000 ms
コード長 712 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 195,648 KB
最終ジャッジ日時 2023-10-20 17:46:48
合計ジャッジ時間 7,355 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,492 KB
testcase_01 AC 37 ms
53,492 KB
testcase_02 AC 38 ms
53,492 KB
testcase_03 AC 37 ms
53,492 KB
testcase_04 AC 38 ms
53,492 KB
testcase_05 AC 37 ms
53,492 KB
testcase_06 AC 37 ms
53,492 KB
testcase_07 AC 203 ms
83,524 KB
testcase_08 AC 177 ms
82,092 KB
testcase_09 AC 192 ms
83,136 KB
testcase_10 AC 247 ms
86,564 KB
testcase_11 AC 183 ms
82,072 KB
testcase_12 AC 155 ms
80,724 KB
testcase_13 AC 145 ms
80,172 KB
testcase_14 AC 182 ms
82,584 KB
testcase_15 AC 205 ms
83,620 KB
testcase_16 AC 246 ms
94,628 KB
testcase_17 AC 256 ms
97,808 KB
testcase_18 AC 225 ms
91,948 KB
testcase_19 AC 428 ms
131,340 KB
testcase_20 AC 467 ms
128,456 KB
testcase_21 AC 327 ms
117,144 KB
testcase_22 AC 128 ms
83,892 KB
testcase_23 AC 146 ms
81,068 KB
testcase_24 AC 133 ms
83,032 KB
testcase_25 AC 373 ms
186,008 KB
testcase_26 AC 571 ms
195,648 KB
testcase_27 AC 247 ms
88,644 KB
testcase_28 AC 153 ms
90,508 KB
testcase_29 AC 53 ms
63,928 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):
    # 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