結果

問題 No.2214 Products on Tree
ユーザー roarisroaris
提出日時 2023-03-23 22:12:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,972 ms / 3,000 ms
コード長 866 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 81,776 KB
実行使用メモリ 446,024 KB
最終ジャッジ日時 2023-10-18 19:40:18
合計ジャッジ時間 20,856 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,548 KB
testcase_01 AC 36 ms
53,548 KB
testcase_02 AC 37 ms
53,548 KB
testcase_03 AC 545 ms
115,364 KB
testcase_04 AC 517 ms
116,928 KB
testcase_05 AC 517 ms
114,068 KB
testcase_06 AC 382 ms
102,948 KB
testcase_07 AC 175 ms
84,948 KB
testcase_08 AC 195 ms
83,236 KB
testcase_09 AC 201 ms
87,356 KB
testcase_10 AC 379 ms
102,120 KB
testcase_11 AC 230 ms
90,304 KB
testcase_12 AC 266 ms
92,832 KB
testcase_13 AC 592 ms
118,408 KB
testcase_14 AC 569 ms
117,580 KB
testcase_15 AC 555 ms
117,572 KB
testcase_16 AC 576 ms
117,608 KB
testcase_17 AC 566 ms
117,636 KB
testcase_18 AC 269 ms
111,124 KB
testcase_19 AC 292 ms
114,656 KB
testcase_20 AC 269 ms
110,356 KB
testcase_21 AC 225 ms
103,244 KB
testcase_22 AC 360 ms
125,948 KB
testcase_23 AC 37 ms
53,548 KB
testcase_24 AC 37 ms
53,548 KB
testcase_25 AC 37 ms
53,548 KB
testcase_26 AC 37 ms
53,548 KB
testcase_27 AC 37 ms
53,548 KB
testcase_28 AC 1,169 ms
373,020 KB
testcase_29 AC 221 ms
89,148 KB
testcase_30 AC 248 ms
161,624 KB
testcase_31 AC 405 ms
161,012 KB
testcase_32 AC 380 ms
142,952 KB
testcase_33 AC 1,668 ms
445,068 KB
testcase_34 AC 1,972 ms
446,024 KB
testcase_35 AC 1,412 ms
306,768 KB
testcase_36 AC 1,411 ms
306,648 KB
testcase_37 AC 246 ms
115,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2*10**5+10)

def dfs(v, pv=-1):
    a, b = [], []
    
    for nv in G[v]:
        if nv==pv:
            continue
        
        dfs(nv, v)
        a.append(dp[nv][0])
        b.append(dp[nv][1])
    
    mult_l = [1]
    mult_r = [1]
    
    for i in range(len(a)):
        mult_l.append(mult_l[-1]*(a[i]+b[i])%MOD)
        mult_r.append(mult_r[-1]*(a[len(a)-1-i]+b[len(a)-1-i])%MOD)
        dp[v][0] = dp[v][0]*(a[i]+b[i])%MOD
        dp[v][1] = dp[v][1]*(a[i]+b[i])%MOD
    
    for i in range(len(a)):
        dp[v][1] = (dp[v][1]+mult_l[i]*mult_r[len(a)-1-i]%MOD*b[i]%MOD)%MOD

N = int(input())
G = [[] for _ in range(N)]

for _ in range(N-1):
    A, B = map(int, input().split())
    G[A-1].append(B-1)
    G[B-1].append(A-1)

dp = [[1]*2 for _ in range(N)]
MOD = 998244353
dfs(0)
print(dp[0][1])
0