結果

問題 No.196 典型DP (1)
ユーザー neterukunneterukun
提出日時 2019-06-15 17:01:41
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,316 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 114,304 KB
最終ジャッジ日時 2024-04-29 19:58:41
合計ジャッジ時間 7,101 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,352 KB
testcase_01 AC 39 ms
52,096 KB
testcase_02 AC 38 ms
51,840 KB
testcase_03 AC 38 ms
52,224 KB
testcase_04 AC 38 ms
52,096 KB
testcase_05 AC 40 ms
52,096 KB
testcase_06 AC 38 ms
52,352 KB
testcase_07 AC 39 ms
51,968 KB
testcase_08 AC 39 ms
52,480 KB
testcase_09 AC 38 ms
52,224 KB
testcase_10 AC 39 ms
52,736 KB
testcase_11 AC 44 ms
58,624 KB
testcase_12 AC 45 ms
58,496 KB
testcase_13 AC 47 ms
59,520 KB
testcase_14 AC 48 ms
60,544 KB
testcase_15 AC 67 ms
71,296 KB
testcase_16 AC 95 ms
80,384 KB
testcase_17 AC 135 ms
86,528 KB
testcase_18 AC 173 ms
94,432 KB
testcase_19 AC 195 ms
99,456 KB
testcase_20 AC 269 ms
110,080 KB
testcase_21 AC 258 ms
110,376 KB
testcase_22 AC 272 ms
110,088 KB
testcase_23 AC 221 ms
114,304 KB
testcase_24 AC 218 ms
112,768 KB
testcase_25 AC 225 ms
113,152 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 195 ms
108,416 KB
testcase_32 AC 196 ms
108,416 KB
testcase_33 AC 195 ms
108,416 KB
testcase_34 AC 197 ms
108,540 KB
testcase_35 AC 197 ms
108,544 KB
testcase_36 AC 202 ms
108,760 KB
testcase_37 AC 190 ms
109,132 KB
testcase_38 AC 193 ms
108,672 KB
testcase_39 AC 179 ms
108,544 KB
testcase_40 AC 192 ms
108,672 KB
testcase_41 AC 37 ms
52,096 KB
testcase_42 AC 38 ms
52,352 KB
testcase_43 AC 38 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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