結果

問題 No.196 典型DP (1)
ユーザー tsutajtsutaj
提出日時 2018-06-09 12:48:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,130 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 53,760 KB
実行使用メモリ 34,052 KB
最終ジャッジ日時 2023-09-13 01:47:22
合計ジャッジ時間 2,235 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 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,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
7,628 KB
testcase_16 AC 4 ms
13,772 KB
testcase_17 AC 6 ms
19,964 KB
testcase_18 AC 9 ms
26,152 KB
testcase_19 AC 9 ms
28,220 KB
testcase_20 AC 12 ms
32,512 KB
testcase_21 AC 12 ms
32,484 KB
testcase_22 AC 12 ms
32,544 KB
testcase_23 AC 12 ms
33,076 KB
testcase_24 AC 13 ms
32,812 KB
testcase_25 AC 13 ms
32,892 KB
testcase_26 AC 15 ms
34,020 KB
testcase_27 AC 15 ms
34,052 KB
testcase_28 AC 14 ms
33,980 KB
testcase_29 AC 14 ms
33,936 KB
testcase_30 AC 14 ms
34,016 KB
testcase_31 AC 13 ms
32,520 KB
testcase_32 AC 13 ms
32,476 KB
testcase_33 AC 13 ms
32,512 KB
testcase_34 AC 13 ms
32,524 KB
testcase_35 AC 12 ms
32,464 KB
testcase_36 AC 13 ms
32,516 KB
testcase_37 AC 11 ms
32,532 KB
testcase_38 AC 12 ms
32,508 KB
testcase_39 AC 13 ms
32,472 KB
testcase_40 AC 13 ms
32,468 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
using Graph = vector< vector<int> >;

const long long int MOD = 1000000007LL;
int N, K, num[2010];
long long int dp[2010][2010];

void dfs(Graph &G, int cur, int par=-1) {
    num[cur] = 1;
    for(auto to : G[cur]) {
        if(to == par) continue;
        dfs(G, to, cur);
        num[cur] += num[to];
    }
}

void solve(Graph &G, int cur, int par=-1) {
    // 全く塗らない、全部塗る
    dp[cur][0] = dp[cur][ num[cur] ] = 1;
    int sum = 0;
    for(auto to : G[cur]) {
        if(to == par) continue;
        // cur は塗らない
        solve(G, to, cur);
        for(int k=sum; k>=0; k--) {
            for(int pt=num[to]; pt>=1; pt--) {
                (dp[cur][k+pt] += dp[cur][k] * dp[to][pt]) %= MOD;
            }
        }
        sum += num[to];
    }
}

int main() {
    scanf("%d%d", &N, &K);

    Graph G(N);
    for(int i=0; i<N-1; i++) {
        int u, v; scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    dfs(G, 0);
    solve(G, 0);

    printf("%lld\n", dp[0][K]);
    return 0;
}
0