結果

問題 No.196 典型DP (1)
ユーザー ayaoniayaoni
提出日時 2021-06-27 19:38:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 86,528 KB
最終ジャッジ日時 2024-06-25 11:55:45
合計ジャッジ時間 5,645 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,K = MI()
Graph = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = MI()
    Graph[a].append(b)
    Graph[b].append(a)
mod = 10**9+7


def DP(u,par):
    dp = [1,1]  # dp[i] = 頂点uを根とする部分木において、丁度i個の頂点が黒に塗られている様な塗り方
    for v in Graph[u]:
        if v == par:
            continue
        dp_v = DP(v,u)
        a = len(dp)
        b = len(dp_v)
        new_dp = [0]*(a+b-1)
        for i in range(a):
            for j in range(b):
                if i != a-1:
                    new_dp[i+j] += dp[i]*dp_v[j]
                    new_dp[i+j] %= mod
        new_dp[-1] = 1
        dp = new_dp
    return dp


ans = DP(0,-1)[K]
print(ans)
0