結果

問題 No.196 典型DP (1)
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-14 06:16:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,396 bytes
コンパイル時間 552 ms
コンパイル使用メモリ 70,812 KB
実行使用メモリ 68,076 KB
最終ジャッジ日時 2023-08-25 17:07:07
合計ジャッジ時間 6,961 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
5,764 KB
testcase_14 AC 2 ms
5,744 KB
testcase_15 AC 16 ms
13,924 KB
testcase_16 AC 54 ms
24,304 KB
testcase_17 AC 103 ms
34,744 KB
testcase_18 AC 324 ms
47,056 KB
testcase_19 AC 399 ms
55,100 KB
testcase_20 AC 235 ms
63,328 KB
testcase_21 AC 439 ms
61,548 KB
testcase_22 AC 384 ms
63,360 KB
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 <= k; 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