結果

問題 No.2047 Path Factory
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2022-08-19 22:28:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,297 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 107,124 KB
最終ジャッジ日時 2024-04-17 01:08:22
合計ジャッジ時間 6,576 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,224 KB
testcase_01 AC 32 ms
52,096 KB
testcase_02 AC 32 ms
52,352 KB
testcase_03 AC 32 ms
52,608 KB
testcase_04 AC 32 ms
52,352 KB
testcase_05 WA -
testcase_06 AC 34 ms
52,352 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 294 ms
94,604 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 206 ms
85,760 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 250 ms
88,736 KB
testcase_20 AC 349 ms
94,928 KB
testcase_21 AC 215 ms
87,500 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 126 ms
78,708 KB
testcase_25 AC 264 ms
97,908 KB
testcase_26 AC 342 ms
98,192 KB
testcase_27 AC 277 ms
101,148 KB
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #


def sum2(a):
    n=len(a)
    ans=sum(a)**2
    for x in a:ans-=x**2
    ans//=2
    return ans
mod=998244353
n=int(input())
root=[[] for i in range(n+3)]
for i in range(n-1):
    a,b=map(int,input().split())
    root[a].append(b)
    root[b].append(a)
oyl=[]
seen=[0]*(n+1)
p=[10**10]*(n+1)
stc=[1]
while stc:
    now=stc.pop()
    oyl.append(now)
    if p[now] in root[now]:root[now].remove(p[now])
    for to in root[now]:
        if to==p[now]:continue
        if seen[to]:continue
        seen[to]=1
        stc.append(to)
        p[to]=now

oyl.reverse()
dp0=[0]*(n+3)
dp1=[0]*(n+3)
dp2=[0]*(n+3)
for v in oyl:
    if len(root[v])==0:
       dp1[v]=1
       continue
    dp1[v]=1
    for to in root[v]:
        dp1[v]*=(dp0[to]+dp2[to])
        dp1[v]%=mod
    cnt0=0
    mul=1
    for to in root[v]:
        if dp0[to]+dp2[to]==0:cnt0+=1
        else:
            mul*=dp0[to]+dp2[to]
            mul%=mod
    for to in root[v]:
        mul2=mul
        if dp0[to]+dp2[to]>0:
            mul2=mul*pow(dp0[to]+dp2[to],mod-2,mod)%mod
        if cnt0>=2:continue
        if cnt0==1 and dp0[to]+dp2[to]!=0:continue
        dp0[v]+=(dp0[to]+dp1[to])*mul2
        dp0[v]%=mod
    a=[]
    for to in root[v]:
        a.append(dp0[to]+dp1[to])
    dp2[v]=sum2(a)%mod

print((dp0[1]+dp2[1])%mod)

0