結果

問題 No.196 典型DP (1)
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-26 19:31:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,000 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 80,624 KB
最終ジャッジ日時 2023-10-25 03:54:21
合計ジャッジ時間 6,331 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,512 KB
testcase_01 AC 38 ms
53,512 KB
testcase_02 AC 38 ms
53,512 KB
testcase_03 AC 38 ms
53,512 KB
testcase_04 AC 37 ms
53,512 KB
testcase_05 AC 37 ms
53,512 KB
testcase_06 AC 39 ms
53,512 KB
testcase_07 AC 39 ms
53,512 KB
testcase_08 AC 39 ms
53,512 KB
testcase_09 AC 39 ms
53,512 KB
testcase_10 AC 39 ms
53,512 KB
testcase_11 AC 42 ms
59,652 KB
testcase_12 AC 44 ms
60,052 KB
testcase_13 AC 45 ms
59,904 KB
testcase_14 AC 45 ms
59,904 KB
testcase_15 AC 50 ms
64,160 KB
testcase_16 AC 61 ms
66,404 KB
testcase_17 AC 72 ms
71,260 KB
testcase_18 AC 76 ms
73,308 KB
testcase_19 AC 82 ms
74,672 KB
testcase_20 AC 85 ms
73,436 KB
testcase_21 AC 116 ms
76,804 KB
testcase_22 AC 90 ms
76,808 KB
testcase_23 AC 101 ms
78,192 KB
testcase_24 AC 98 ms
77,756 KB
testcase_25 AC 97 ms
77,588 KB
testcase_26 AC 104 ms
80,392 KB
testcase_27 AC 101 ms
80,176 KB
testcase_28 AC 98 ms
80,152 KB
testcase_29 AC 102 ms
79,800 KB
testcase_30 AC 96 ms
80,624 KB
testcase_31 AC 139 ms
76,656 KB
testcase_32 AC 144 ms
76,656 KB
testcase_33 AC 140 ms
76,656 KB
testcase_34 AC 145 ms
76,656 KB
testcase_35 AC 140 ms
76,656 KB
testcase_36 AC 138 ms
76,652 KB
testcase_37 AC 93 ms
76,748 KB
testcase_38 AC 142 ms
76,744 KB
testcase_39 AC 127 ms
76,708 KB
testcase_40 AC 141 ms
76,484 KB
testcase_41 AC 37 ms
53,516 KB
testcase_42 AC 37 ms
53,516 KB
testcase_43 AC 37 ms
53,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7

N, K = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)

# topoligical sort
par = [-1] * N
topo = []
que = [0]
while que:
    s = que.pop()
    topo.append(s)
    for t in edge[s]:
        if t == par[s]:
            continue
        par[t] = s
        que.append(t)


dp = [-1] * N  # dp[v][i], vが根の部分木で黒で塗るノードがi個の数
sz = [0] * N
for s in topo[::-1]:
    sz[s] = 1
    dp[s] = [0] * 2
    dp[s][0] = 1
    for t in edge[s]:
        if t == par[s]:
            continue
        merge = [0] * (sz[s] + sz[t] + 1)
        for i, x in enumerate(dp[s]):
            for j, y in enumerate(dp[t]):
                merge[i+j] += x * y
                merge[i+j] %= mod
        sz[s] += sz[t]
        dp[s] = merge
    dp[s][sz[s]] = dp[s][sz[s] - 1]

print(dp[0][K])
0