結果

問題 No.196 典型DP (1)
ユーザー face4face4
提出日時 2019-11-25 10:24:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 951 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 70,900 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-04-20 22:45:58
合計ジャッジ時間 2,344 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 5 ms
6,528 KB
testcase_17 AC 9 ms
7,936 KB
testcase_18 AC 12 ms
9,600 KB
testcase_19 AC 14 ms
10,112 KB
testcase_20 AC 19 ms
11,904 KB
testcase_21 AC 19 ms
11,648 KB
testcase_22 AC 20 ms
11,648 KB
testcase_23 AC 23 ms
14,592 KB
testcase_24 AC 21 ms
13,568 KB
testcase_25 AC 21 ms
13,312 KB
testcase_26 AC 26 ms
17,664 KB
testcase_27 AC 26 ms
17,664 KB
testcase_28 AC 25 ms
17,664 KB
testcase_29 AC 26 ms
17,664 KB
testcase_30 AC 26 ms
17,792 KB
testcase_31 AC 22 ms
11,776 KB
testcase_32 AC 21 ms
11,648 KB
testcase_33 AC 21 ms
11,776 KB
testcase_34 AC 21 ms
11,648 KB
testcase_35 AC 22 ms
11,776 KB
testcase_36 AC 22 ms
11,776 KB
testcase_37 AC 19 ms
11,648 KB
testcase_38 AC 23 ms
11,776 KB
testcase_39 AC 21 ms
11,776 KB
testcase_40 AC 22 ms
11,776 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

typedef long long ll;
const int mod = 1e9+7;
vector<int> v[2001];
int dp[2001][2001] = {}, child[2001] = {};

void dfs(int x, int p){
    child[x] = 1;
    for(int next : v[x]){
        if(next == p)   continue;
        dfs(next, x);
        child[x] += child[next];
    }
}

void dfs2(int x, int p){
    dp[x][0] = 1;
    for(int next : v[x]){
        if(next == p)   continue;
        dfs2(next, x);
        for(int i = child[x]-child[next]; i >= 0; i--){
            for(int j = child[next]; j > 0; j--){
                (dp[x][i+j] += (ll)dp[x][i]*dp[next][j]%mod) %= mod;
            }
        }
    }
    dp[x][child[x]] = 1;
}

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(0, -1);
    dfs2(0, -1);
    cout << dp[0][k] << endl;
    return 0;
}
0