結果

問題 No.196 典型DP (1)
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-14 06:18:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,403 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 70,484 KB
実行使用メモリ 68,088 KB
最終ジャッジ日時 2023-08-25 17:07:20
合計ジャッジ時間 8,188 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
5,984 KB
testcase_14 AC 3 ms
5,736 KB
testcase_15 AC 17 ms
13,928 KB
testcase_16 AC 24 ms
24,176 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
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 <iostream>
#include <vector>
using namespace std;

using int64 = long long;

const int64 MOD = 1e9 + 7;

vector<int> T[2010];

int64 dp[2010][2010][2];
int size[2010];

int N, K;

int64 dfs(int v, int par) {
    size[v] = 1;

    dp[v][0][0] = dp[v][1][1] = 1;

    for (int to : T[v]) {
        if (to == par) continue;
        dfs(to, v);
        size[v] += size[to];
        int num = size[v];

        for (int k = num; k >= 0; k--) {
            for (int st = 0; st <= 1; st++) {
                if (k == 0 and st == 1) continue;

                int64 nxt = 0;
                for (int subk = 0; subk <= size[to]; subk++) {
                    for (int subst = 0; subst <= 1; subst++) {

                        if (st == 1 and subst == 0) continue;
                        if (st == 1 and subk + 1 > k) continue;
                        (nxt += dp[v][k - subk][st] * dp[to][subk][subst]) %= MOD;
                    }
                }
                dp[v][k][st] = nxt;
            }
        }
    }
    return (dp[v][K][0] + dp[v][K][1]) % MOD;
}

int main() {
    cin >> N >> K;

    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;

        T[a].push_back(b);
        T[b].push_back(a);
    }

    cout << dfs(0, -1) << endl;

    for (int st = 0; st <= 1; st++) {
        cerr << "st: " << st << " dp: " << dp[0][K][st] << endl;
    }
    
    return 0;
}
0