結果

問題 No.196 典型DP (1)
ユーザー titia
提出日時 2022-06-20 02:15:54
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,012 bytes
コンパイル時間 367 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 40,576 KB
最終ジャッジ日時 2024-10-12 07:52:59
合計ジャッジ時間 42,105 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33 TLE * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
            
        
        

    
0