結果
問題 | No.196 典型DP (1) |
ユーザー | titia |
提出日時 | 2022-06-20 02:16:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 167 ms / 2,000 ms |
コード長 | 1,048 bytes |
コンパイル時間 | 325 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 87,936 KB |
最終ジャッジ日時 | 2024-10-12 07:55:10 |
合計ジャッジ時間 | 5,558 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
# いわゆる「二乗の木DP」 N,K=map(int,input().split()) E=[[] for i in range(N)] mod=10**9+7 for i in range(N-1): a,b=map(int,input().split()) E[a].append(b) E[b].append(a) ROOT=0 QUE=[ROOT] Parent=[-1]*N Parent[ROOT]=N # ROOTの親を定めておく. Child=[[] for i in range(N)] TOP_SORT=[] # トポロジカルソート while QUE: # トポロジカルソートと同時に親を見つける x=QUE.pop() TOP_SORT.append(x) for to in E[x]: if Parent[to]==-1: Parent[to]=x Child[x].append(to) QUE.append(to) DP=[[] for i in range(N)] for x in TOP_SORT[::-1]: if len(Child[x])==0: DP[x]=[1,1] continue NDP=[1] for to in Child[x]: XDP=[0]*(len(DP[to])+len(NDP)-1) for i in range(len(DP[to])): for j in range(len(NDP)): XDP[i+j]+=DP[to][i]*NDP[j]%mod XDP[i+j]%=mod NDP=XDP NDP.append(1) DP[x]=NDP print(DP[0][K]%mod)