結果

問題 No.196 典型DP (1)
ユーザー veqccveqcc
提出日時 2019-02-21 21:38:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,421 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 95,556 KB
実行使用メモリ 66,208 KB
最終ジャッジ日時 2024-05-01 05:13:26
合計ジャッジ時間 9,614 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 4 ms
9,760 KB
testcase_16 AC 7 ms
14,288 KB
testcase_17 AC 11 ms
20,492 KB
testcase_18 AC 25 ms
26,600 KB
testcase_19 AC 17 ms
28,800 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 493 ms
42,068 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 TLE -
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 (auto child : edge[cur]) {
        if (child == par) continue;

        int sz = dfs(child, cur);
        sm += sz;
        for (int j = 0; j <= sz; 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 <= sm; 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