結果

問題 No.196 典型DP (1)
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-23 15:03:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 2,000 ms
コード長 1,601 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 178,036 KB
実行使用メモリ 98,076 KB
最終ジャッジ日時 2023-09-29 02:04:09
合計ジャッジ時間 11,031 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 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,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 31 ms
4,380 KB
testcase_17 AC 74 ms
4,380 KB
testcase_18 AC 140 ms
5,128 KB
testcase_19 AC 175 ms
5,352 KB
testcase_20 AC 275 ms
5,080 KB
testcase_21 AC 267 ms
5,356 KB
testcase_22 AC 270 ms
5,552 KB
testcase_23 AC 336 ms
38,072 KB
testcase_24 AC 311 ms
27,404 KB
testcase_25 AC 305 ms
24,328 KB
testcase_26 AC 411 ms
98,028 KB
testcase_27 AC 413 ms
97,956 KB
testcase_28 AC 412 ms
97,968 KB
testcase_29 AC 413 ms
97,932 KB
testcase_30 AC 411 ms
98,076 KB
testcase_31 AC 353 ms
4,376 KB
testcase_32 AC 353 ms
4,376 KB
testcase_33 AC 351 ms
4,392 KB
testcase_34 AC 347 ms
4,380 KB
testcase_35 AC 348 ms
4,384 KB
testcase_36 AC 329 ms
4,380 KB
testcase_37 AC 195 ms
4,380 KB
testcase_38 AC 330 ms
4,376 KB
testcase_39 AC 285 ms
4,380 KB
testcase_40 AC 343 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; }
int modinv(int a) { return modpow(a, mod - 2); }
//-----------------------------------------------------------------------------------
int N, K;
vector<int> E[2010];
//-----------------------------------------------------------------------------------
int cnt[2010]; map<int,int> dp[2010];
void merge(int cu, int to) { // dp[cu] = dp[cu] + dp[to]
    map<int, int> cnt;
    for (auto p : dp[cu]) for (auto q : dp[to]) {
        cnt[p.first + q.first] = add(cnt[p.first + q.first], mul(p.second, q.second));
    }
    dp[cu] = cnt;
}
void dfs(int cu, int pa = -1) {
    dp[cu][0] = 1;

    cnt[cu] = 1;
    for (int to : E[cu]) if (pa != to) {
        dfs(to, cu);
        cnt[cu] += cnt[to];

        merge(cu, to);
    }

    dp[cu][cnt[cu]] = 1;
}
//-----------------------------------------------------------------------------------
int main() {
    cin >> N >> K;
    rep(i, 0, N - 1) {
        int a, b; scanf("%d%d", &a, &b);
        E[a].push_back(b); E[b].push_back(a);
    }

    dfs(0);
    printf("%d\n", dp[0][K]);
}
0