結果

問題 No.196 典型DP (1)
ユーザー veqccveqcc
提出日時 2019-02-21 22:11:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 97,208 KB
実行使用メモリ 89,216 KB
最終ジャッジ日時 2024-05-01 05:43:22
合計ジャッジ時間 3,299 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 5 ms
5,760 KB
testcase_16 AC 7 ms
6,912 KB
testcase_17 AC 10 ms
8,704 KB
testcase_18 AC 14 ms
10,496 KB
testcase_19 AC 16 ms
11,392 KB
testcase_20 AC 21 ms
12,416 KB
testcase_21 AC 20 ms
12,672 KB
testcase_22 AC 20 ms
12,672 KB
testcase_23 AC 44 ms
44,672 KB
testcase_24 AC 36 ms
32,768 KB
testcase_25 AC 33 ms
31,360 KB
testcase_26 AC 73 ms
89,088 KB
testcase_27 AC 75 ms
89,088 KB
testcase_28 AC 73 ms
88,960 KB
testcase_29 AC 72 ms
89,088 KB
testcase_30 AC 73 ms
89,216 KB
testcase_31 AC 25 ms
11,648 KB
testcase_32 AC 25 ms
11,648 KB
testcase_33 AC 25 ms
11,776 KB
testcase_34 AC 25 ms
11,776 KB
testcase_35 AC 25 ms
11,648 KB
testcase_36 AC 24 ms
11,776 KB
testcase_37 AC 20 ms
12,160 KB
testcase_38 AC 25 ms
11,776 KB
testcase_39 AC 23 ms
11,904 KB
testcase_40 AC 25 ms
11,648 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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][2005]; // 子をx番目まで見たときに、頂点i以下でちょうどj個を黒にする場合の数
    fill(dp2[0], dp2[1]+2005, 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);
        for (int j = 0; j <= sz; j++) {
            for (int k = 0; k < sm; k++) {
                (dp2[y][j+k] += dp2[x][k] * dp[child][j] % MOD) %= MOD;
            }
        }

        sm += sz;
        swap(x, y);
        fill(dp2[y], dp2[y]+2005, 0);
    }

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

    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