結果

問題 No.196 典型DP (1)
ユーザー tsutajtsutaj
提出日時 2018-06-09 12:55:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,205 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 55,344 KB
実行使用メモリ 34,364 KB
最終ジャッジ日時 2023-09-13 01:48:08
合計ジャッジ時間 3,268 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 RE -
testcase_04 AC 1 ms
4,376 KB
testcase_05 RE -
testcase_06 AC 1 ms
4,376 KB
testcase_07 RE -
testcase_08 AC 2 ms
4,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 RE -
testcase_16 AC 2 ms
4,376 KB
testcase_17 RE -
testcase_18 AC 5 ms
4,376 KB
testcase_19 RE -
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 29 ms
16,464 KB
testcase_24 AC 23 ms
11,460 KB
testcase_25 AC 20 ms
11,016 KB
testcase_26 AC 42 ms
34,308 KB
testcase_27 AC 41 ms
34,348 KB
testcase_28 AC 41 ms
34,364 KB
testcase_29 AC 42 ms
34,292 KB
testcase_30 AC 41 ms
34,308 KB
testcase_31 AC 7 ms
4,376 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 7 ms
4,380 KB
testcase_34 AC 7 ms
4,376 KB
testcase_35 AC 7 ms
4,376 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 7 ms
4,376 KB
testcase_38 AC 7 ms
4,376 KB
testcase_39 AC 7 ms
4,376 KB
testcase_40 AC 7 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 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];

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];
    }
}

vector<long long int> solve(Graph &G, int cur, int par=-1) {
    // 全く塗らない、全部塗る
    vector<long long int> dp(N, 0);
    dp[0] = dp[ num[cur] ] = 1;

    int sum = 0;
    for(auto to : G[cur]) {
        if(to == par) continue;
        // cur は塗らない
        vector<long long int> child = solve(G, to, cur);

        for(int k=sum; k>=0; k--) {
            for(int pt=num[to]; pt>=1; pt--) {
                (dp[k+pt] += dp[k] * child[pt]) %= MOD;
            }
        }
        sum += num[to];
    }
    return dp;
}

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);
    vector<long long int> ans = solve(G, 0);

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