結果

問題 No.196 典型DP (1)
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-14 06:44:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,460 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 72,020 KB
実行使用メモリ 128,364 KB
最終ジャッジ日時 2023-08-25 17:07:39
合計ジャッジ時間 2,902 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 61 ms
126,412 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 15 ms
63,340 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 17 ms
63,456 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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);

        int64 values[2010][2] = {};

        for (int k = min(K, size[v]); k >= 0; k--) {
            for (int st = 0; st <= 1; st++) {
                if (k == 0 and st == 1) continue;

                for (int subk = 0; subk + K <= min(K, size[to]); subk++) {
                    for (int subst = 0; subst <= 1; subst++) {
                        if (st == 1 and subst == 0) continue;
                        (values[k + subk][st] += dp[v][k][st] * dp[to][subk][subst]) %= MOD;
                    }
                }
            }
        }
        // valuesをdpに移す
        size[v] += size[to];
        for (int k = 0; k <= min(K, size[v]); k++) for (int st = 0; st <= 1; st++) dp[v][k][st] = values[k][st];
    }
    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