結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-12-31 00:52:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 82,052 KB
実行使用メモリ 110,324 KB
最終ジャッジ日時 2024-04-28 02:08:23
合計ジャッジ時間 5,665 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
51,840 KB
testcase_01 AC 35 ms
51,968 KB
testcase_02 AC 33 ms
52,352 KB
testcase_03 AC 31 ms
52,096 KB
testcase_04 AC 31 ms
52,096 KB
testcase_05 AC 31 ms
51,968 KB
testcase_06 AC 32 ms
51,712 KB
testcase_07 AC 32 ms
51,712 KB
testcase_08 AC 32 ms
51,712 KB
testcase_09 AC 31 ms
52,480 KB
testcase_10 AC 32 ms
52,608 KB
testcase_11 AC 35 ms
58,496 KB
testcase_12 AC 38 ms
60,160 KB
testcase_13 AC 38 ms
59,520 KB
testcase_14 AC 40 ms
60,288 KB
testcase_15 AC 47 ms
65,792 KB
testcase_16 AC 57 ms
72,704 KB
testcase_17 AC 81 ms
85,504 KB
testcase_18 AC 101 ms
94,072 KB
testcase_19 AC 113 ms
98,452 KB
testcase_20 AC 131 ms
102,784 KB
testcase_21 AC 149 ms
108,672 KB
testcase_22 AC 123 ms
102,528 KB
testcase_23 AC 143 ms
110,080 KB
testcase_24 AC 135 ms
109,056 KB
testcase_25 AC 137 ms
109,080 KB
testcase_26 AC 149 ms
109,920 KB
testcase_27 AC 151 ms
110,080 KB
testcase_28 AC 153 ms
110,324 KB
testcase_29 AC 150 ms
110,080 KB
testcase_30 AC 150 ms
109,952 KB
testcase_31 AC 168 ms
108,160 KB
testcase_32 AC 180 ms
108,480 KB
testcase_33 AC 168 ms
108,492 KB
testcase_34 AC 168 ms
108,544 KB
testcase_35 AC 166 ms
108,288 KB
testcase_36 AC 168 ms
108,160 KB
testcase_37 AC 131 ms
108,544 KB
testcase_38 AC 167 ms
108,492 KB
testcase_39 AC 159 ms
108,544 KB
testcase_40 AC 169 ms
108,476 KB
testcase_41 AC 32 ms
52,608 KB
testcase_42 AC 32 ms
52,096 KB
testcase_43 AC 31 ms
51,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)


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 = [[0]*(n+1) for i in range(n)]

cnt_v = [1] *n
def solve(prev_pos, pos):
    # dfsで辺を葉の方向へと辿る
    for next_pos in tree[pos]:
        if prev_pos == next_pos:
            continue
        solve(pos, next_pos)
    
    # 頂点posに対して、next_posを根とする部分木をマージしていく
    dp[pos][0] = 1
    for next_pos in tree[pos]:
        if prev_pos == next_pos:
            continue
        
        tmp = [0] * (cnt_v[pos] + cnt_v[next_pos] + 1)
        # マージされる側に存在する辺の本数
        for i in range(cnt_v[pos] + 1):
            # マージする側に存在する辺の本数
            for j in range(cnt_v[next_pos] + 1):
                # マージの内容
                tmp[i+j] += dp[pos][i] * dp[next_pos][j]
                tmp[i+j] %= MOD

        cnt_v[pos] += cnt_v[next_pos]
        for j in range(cnt_v[pos]+1):
            dp[pos][j] = tmp[j]
            dp[pos][j] %= MOD
    dp[pos][cnt_v[pos]] = 1

solve(-1, 0)
print(dp[0][k] % MOD)
0