結果

問題 No.196 典型DP (1)
ユーザー veqccveqcc
提出日時 2019-02-21 21:36:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,468 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 97,272 KB
実行使用メモリ 93,984 KB
最終ジャッジ日時 2024-05-01 05:10:30
合計ジャッジ時間 8,210 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,884 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,948 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 4 ms
10,148 KB
testcase_16 AC 6 ms
14,804 KB
testcase_17 AC 9 ms
20,888 KB
testcase_18 AC 23 ms
27,392 KB
testcase_19 AC 18 ms
29,340 KB
testcase_20 AC 9 ms
31,552 KB
testcase_21 AC 10 ms
13,824 KB
testcase_22 AC 8 ms
12,032 KB
testcase_23 AC 230 ms
23,168 KB
testcase_24 AC 22 ms
14,080 KB
testcase_25 AC 593 ms
37,504 KB
testcase_26 AC 12 ms
30,220 KB
testcase_27 AC 163 ms
23,168 KB
testcase_28 AC 1,973 ms
65,804 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

const ll MOD = 1000000007LL;
int n, K;
vector <int> edge[2005];
ll dp[2005][2005]; // 頂点i以下のsubtreeにおいて、ちょうどj個の頂点を黒にする場合の数

int dfs(int cur, int par) {
    ll dp2[2][K+1]; // 子をx番目まで見たときに、頂点i以下でちょうどj個を黒にする場合の数
    fill(dp2[0], dp2[1]+K+1, 0);
    dp2[0][0] = 1;

    int sm = 1;
    int x = 0, y = 1;
    for (int i = 0; i < edge[cur].size(); i++) {
        int child = edge[cur][i];
        if (child == par) continue;

        int sz = dfs(child, cur);
        sm += sz;
        for (int j = sz; j >= 0; j--) {
            for (int k = 0; k <= min(sm, K - j); k++) {
                (dp2[y][j+k] += dp2[x][k] * dp[child][j] % MOD) %= MOD;
            }
        }

        swap(x, y);
        fill(dp2[y], dp2[y]+K+1, 0);
    }

    for (int i = 0; i <= K; i++) dp[cur][i] = dp2[x][i];
    if (sm <= K) (dp[cur][sm] += 1) %= MOD;

    return sm;
}

int main() {
    cin >> n >> K;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }

    dfs(0, -1);
    cout << dp[0][K] << "\n";
    return 0;
}
0