結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-06-15 17:03:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,428 ms / 2,000 ms
コード長 1,358 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 64,512 KB
最終ジャッジ日時 2024-04-29 20:02:14
合計ジャッジ時間 24,219 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,880 KB
testcase_01 AC 30 ms
10,752 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 29 ms
10,880 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 29 ms
10,752 KB
testcase_06 AC 29 ms
11,008 KB
testcase_07 AC 30 ms
10,880 KB
testcase_08 AC 30 ms
10,880 KB
testcase_09 AC 30 ms
11,008 KB
testcase_10 AC 31 ms
10,880 KB
testcase_11 AC 32 ms
10,880 KB
testcase_12 AC 31 ms
10,880 KB
testcase_13 AC 31 ms
10,880 KB
testcase_14 AC 32 ms
10,752 KB
testcase_15 AC 48 ms
11,904 KB
testcase_16 AC 93 ms
14,976 KB
testcase_17 AC 175 ms
20,224 KB
testcase_18 AC 284 ms
28,672 KB
testcase_19 AC 346 ms
33,408 KB
testcase_20 AC 515 ms
43,392 KB
testcase_21 AC 518 ms
43,520 KB
testcase_22 AC 523 ms
43,648 KB
testcase_23 AC 815 ms
64,512 KB
testcase_24 AC 740 ms
57,856 KB
testcase_25 AC 722 ms
56,064 KB
testcase_26 AC 855 ms
43,264 KB
testcase_27 AC 872 ms
43,392 KB
testcase_28 AC 858 ms
43,392 KB
testcase_29 AC 855 ms
43,392 KB
testcase_30 AC 859 ms
43,264 KB
testcase_31 AC 1,428 ms
43,136 KB
testcase_32 AC 1,425 ms
42,752 KB
testcase_33 AC 1,403 ms
43,008 KB
testcase_34 AC 1,426 ms
42,880 KB
testcase_35 AC 1,426 ms
43,008 KB
testcase_36 AC 1,349 ms
42,880 KB
testcase_37 AC 545 ms
43,008 KB
testcase_38 AC 1,350 ms
43,008 KB
testcase_39 AC 1,171 ms
43,136 KB
testcase_40 AC 1,399 ms
42,880 KB
testcase_41 AC 31 ms
10,752 KB
testcase_42 AC 31 ms
10,880 KB
testcase_43 AC 30 ms
10,752 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