結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-06-15 17:02:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 1,359 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 114,560 KB
最終ジャッジ日時 2024-04-29 20:00:41
合計ジャッジ時間 7,388 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,224 KB
testcase_01 AC 41 ms
52,096 KB
testcase_02 AC 42 ms
52,224 KB
testcase_03 AC 41 ms
52,224 KB
testcase_04 AC 41 ms
51,968 KB
testcase_05 AC 43 ms
52,480 KB
testcase_06 AC 40 ms
52,096 KB
testcase_07 AC 42 ms
52,608 KB
testcase_08 AC 42 ms
52,096 KB
testcase_09 AC 46 ms
52,224 KB
testcase_10 AC 41 ms
52,480 KB
testcase_11 AC 47 ms
58,112 KB
testcase_12 AC 48 ms
58,368 KB
testcase_13 AC 50 ms
59,520 KB
testcase_14 AC 53 ms
60,672 KB
testcase_15 AC 77 ms
71,296 KB
testcase_16 AC 107 ms
80,512 KB
testcase_17 AC 153 ms
86,656 KB
testcase_18 AC 179 ms
94,188 KB
testcase_19 AC 210 ms
100,096 KB
testcase_20 AC 277 ms
109,724 KB
testcase_21 AC 266 ms
110,328 KB
testcase_22 AC 278 ms
109,952 KB
testcase_23 AC 225 ms
114,560 KB
testcase_24 AC 223 ms
112,872 KB
testcase_25 AC 233 ms
113,024 KB
testcase_26 AC 185 ms
110,192 KB
testcase_27 AC 185 ms
110,336 KB
testcase_28 AC 183 ms
110,172 KB
testcase_29 AC 187 ms
110,080 KB
testcase_30 AC 198 ms
110,976 KB
testcase_31 AC 203 ms
108,800 KB
testcase_32 AC 207 ms
108,448 KB
testcase_33 AC 204 ms
108,288 KB
testcase_34 AC 205 ms
108,468 KB
testcase_35 AC 209 ms
108,436 KB
testcase_36 AC 208 ms
108,416 KB
testcase_37 AC 202 ms
108,800 KB
testcase_38 AC 205 ms
108,524 KB
testcase_39 AC 198 ms
108,416 KB
testcase_40 AC 207 ms
108,844 KB
testcase_41 AC 40 ms
52,096 KB
testcase_42 AC 40 ms
52,072 KB
testcase_43 AC 39 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(100000)


n, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n-1)]
MOD = 10**9 + 7

tree = [[] for i in range(n)]
for i in range(n-1):
    tree[info[i][0]].append(info[i][1])
    tree[info[i][1]].append(info[i][0])

# dp[pos][j] := 頂点posを根とする部分木から、
# ちょうどj個の頂点を条件2に従って黒で塗る方法の数
dp = [[0]*(n+1) for i in range(n)]

#0個の頂点を黒で塗る通り数は、それぞれの部分木に対して1通り
for i in range(n):
    dp[i][0] = 1

def dfs(pos):
    cnt = 1
    #部分木が葉のとき
    if all([visited[i] for i in tree[pos]]):
        dp[pos][cnt] = 1
        return cnt #cnt = 1

    #部分木が葉でないとき
    for child_pos in tree[pos]:
        if not visited[child_pos]:
            visited[child_pos] = True
            cnt_child = dfs(child_pos)
            tmp = [0]*(cnt + cnt_child + 1)
            for i in range(cnt+1):
                for j in range(cnt_child+1):
                    tmp[i+j] += dp[pos][i] * dp[child_pos][j]
            cnt += cnt_child
            for i in range(cnt+1):
                dp[pos][i] = tmp[i]
                dp[pos][i] %= MOD

    dp[pos][cnt] = 1 
    return cnt

visited = [False] * n
visited[0] = True
dfs(0)
print(dp[0][k] % MOD)
      
        
0