結果

問題 No.196 典型DP (1)
ユーザー uenokuuenoku
提出日時 2017-11-16 21:32:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,459 bytes
コンパイル時間 1,760 ms
コンパイル使用メモリ 170,212 KB
実行使用メモリ 37,680 KB
最終ジャッジ日時 2023-08-16 17:09:47
合計ジャッジ時間 7,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 25 ms
8,216 KB
testcase_16 AC 44 ms
14,656 KB
testcase_17 AC 122 ms
20,784 KB
testcase_18 AC 426 ms
27,052 KB
testcase_19 AC 338 ms
29,020 KB
testcase_20 AC 48 ms
32,912 KB
testcase_21 AC 119 ms
32,980 KB
testcase_22 AC 35 ms
32,868 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
lli mod = 1e9 + 7;
vector<int> e[2005];
lli dp[2005][2005] = {};
lli memo[2005] = {};

int n, k;
// iの部分木でj個色が塗られている場合の数
int dfs(int cur, int par)
{
    if (cur != 0 and e[cur].size() == 1) {
        // leaf
        dp[cur][1] = 1;
        dp[cur][0] = 1;
        memo[cur] = 1;
        return 1;
    }
    int size = 1;
    for (auto s : e[cur]) {
        if (s == par)
            continue;
        size += dfs(s, cur);
    }
    dp[cur][0] = 1;
    for (auto s : e[cur]) {
        if (s == par)
            continue;
        vector<lli> tmp(n + 1, 0);

        rep(i, k + 1)
        {
            tmp[i] = dp[cur][i];
        }
        vector<lli> hoge(n + 1, 0);
        rep(i, memo[s] + 1)
        {
            rep(j, k + 1)
            {
                if (i + j > size)
                    continue;
                hoge[i + j] += (tmp[j] % mod * dp[s][i]) % mod;
                hoge[i + j] %= mod;
            }
        }

        rep(i, k + 1) dp[cur][i] = hoge[i];
    }
    dp[cur][size] = 1;
    memo[cur] = size;
    return size;
}
int main()
{
    cin >> n >> k;
    int a, b;
    rep(i, n - 1)
    {
        cin >> a >> b;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    dfs(0, -1);

    cout << dp[0][k] << endl;
}
0