結果

問題 No.196 典型DP (1)
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-02 23:43:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 1,093 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 85,760 KB
最終ジャッジ日時 2023-09-15 04:29:04
合計ジャッジ時間 7,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,272 KB
testcase_01 AC 70 ms
71,116 KB
testcase_02 AC 71 ms
71,364 KB
testcase_03 AC 71 ms
71,324 KB
testcase_04 AC 72 ms
71,232 KB
testcase_05 AC 71 ms
71,092 KB
testcase_06 AC 73 ms
71,352 KB
testcase_07 AC 71 ms
71,056 KB
testcase_08 AC 72 ms
71,276 KB
testcase_09 AC 73 ms
71,324 KB
testcase_10 AC 73 ms
71,280 KB
testcase_11 AC 78 ms
75,700 KB
testcase_12 AC 73 ms
75,948 KB
testcase_13 AC 78 ms
76,044 KB
testcase_14 AC 79 ms
76,204 KB
testcase_15 AC 89 ms
76,464 KB
testcase_16 AC 85 ms
76,052 KB
testcase_17 AC 103 ms
76,884 KB
testcase_18 AC 114 ms
78,904 KB
testcase_19 AC 120 ms
78,376 KB
testcase_20 AC 126 ms
77,732 KB
testcase_21 AC 132 ms
79,604 KB
testcase_22 AC 124 ms
78,076 KB
testcase_23 AC 144 ms
79,688 KB
testcase_24 AC 132 ms
79,348 KB
testcase_25 AC 143 ms
85,760 KB
testcase_26 AC 147 ms
81,048 KB
testcase_27 AC 144 ms
80,772 KB
testcase_28 AC 149 ms
80,464 KB
testcase_29 AC 147 ms
80,860 KB
testcase_30 AC 144 ms
81,056 KB
testcase_31 AC 167 ms
78,024 KB
testcase_32 AC 167 ms
77,588 KB
testcase_33 AC 166 ms
77,920 KB
testcase_34 AC 167 ms
77,928 KB
testcase_35 AC 167 ms
77,700 KB
testcase_36 AC 169 ms
78,160 KB
testcase_37 AC 129 ms
78,068 KB
testcase_38 AC 175 ms
77,924 KB
testcase_39 AC 163 ms
77,976 KB
testcase_40 AC 168 ms
78,372 KB
testcase_41 AC 73 ms
71,088 KB
testcase_42 AC 71 ms
71,352 KB
testcase_43 AC 68 ms
71,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(int(1e9))
input = lambda: sys.stdin.readline().rstrip("\r\n")
MOD = int(1e9 + 7)

if __name__ == "__main__":
    n, k = map(int, input().split())
    adjList = [[] for _ in range(n)]
    for _ in range(n - 1):
        u, v = map(int, input().split())
        adjList[u].append(v)
        adjList[v].append(u)

    def dfs(cur: int, pre: int) -> None:
        subSize[cur] = 1
        dp[cur] = [1, 1]
        for next in adjList[cur]:
            if next == pre:
                continue
            dfs(next, cur)
            merged = [0] * (subSize[cur] + subSize[next] + 1)
            for i in range(subSize[cur] + 1):
                for j in range(subSize[next] + 1):
                    if i != subSize[cur]:  # 不涂黑当前节点
                        merged[i + j] += dp[cur][i] * dp[next][j]
                        merged[i + j] %= MOD
            subSize[cur] += subSize[next]
            dp[cur] = merged
        dp[cur][-1] = 1  # 涂黑当前节点

    subSize = [0] * n
    dp = [[] for _ in range(n)]
    dfs(0, -1)
    print(dp[0][k])
0