結果
問題 | No.2214 Products on Tree |
ユーザー |
![]() |
提出日時 | 2023-02-14 05:11:50 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 2,230 ms / 3,000 ms |
コード長 | 1,515 bytes |
コンパイル時間 | 364 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 87,740 KB |
最終ジャッジ日時 | 2024-07-16 16:05:44 |
合計ジャッジ時間 | 43,500 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
import sysinput = sys.stdin.readlineN=int(input())E=[[] for i in range(N)]mod=998244353for i in range(N-1):a,b=map(int,input().split())a-=1b-=1E[a].append(b)E[b].append(a)ROOT=0QUE=[ROOT]Parent=[-1]*(N+1)Parent[ROOT]=N # ROOTの親を定めておく.Child=[[] for i in range(N+1)]TOP_SORT=[] # トポロジカルソートwhile QUE: # トポロジカルソートと同時に親を見つけるx=QUE.pop()TOP_SORT.append(x)for to in E[x]:if Parent[to]==-1:Parent[to]=xChild[x].append(to)QUE.append(to)DP1=[0]*NDP2=[0]*Nfor x in TOP_SORT[::-1]:if len(Child[x])==0:DP1[x]=1DP2[x]=1continueARI=[]NASHI=[]ANS=1for c in Child[x]:ARI.append(DP1[c])NASHI.append(DP2[c])ANS*=DP1[c]+DP2[c]ANS%=modDP2[x]=ANSLEFT=[1]*(len(NASHI)+1)RIGHT=[1]*(len(NASHI)+1)LEFT[0]=NASHI[0]+ARI[0]for i in range(1,len(NASHI)):LEFT[i]=LEFT[i-1]*(NASHI[i]+ARI[i])%modRIGHT[len(NASHI)-1]=NASHI[len(NASHI)-1]+ARI[len(NASHI)-1]for i in range(len(NASHI)-2,-1,-1):RIGHT[i]=RIGHT[i+1]*(NASHI[i]+ARI[i])%modANS=0for i in range(len(NASHI)):if i==0:ANS+=ARI[i]*RIGHT[i+1]elif i==len(NASHI)-1:ANS+=ARI[i]*LEFT[i-1]else:ANS+=LEFT[i-1]*ARI[i]*RIGHT[i+1]ANS%=modDP1[x]=ANS+DP2[x]print((DP1[0])%mod)