結果

問題 No.196 典型DP (1)
ユーザー pyonthpyonth
提出日時 2020-12-03 22:23:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,069 ms / 2,000 ms
コード長 1,627 bytes
コンパイル時間 3,091 ms
コンパイル使用メモリ 209,300 KB
実行使用メモリ 35,056 KB
最終ジャッジ日時 2023-10-12 08:40:54
合計ジャッジ時間 12,408 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 9 ms
7,052 KB
testcase_17 AC 19 ms
12,092 KB
testcase_18 AC 35 ms
19,604 KB
testcase_19 AC 49 ms
24,476 KB
testcase_20 AC 60 ms
34,444 KB
testcase_21 AC 61 ms
34,456 KB
testcase_22 AC 64 ms
34,456 KB
testcase_23 AC 734 ms
34,664 KB
testcase_24 AC 548 ms
34,644 KB
testcase_25 AC 454 ms
34,652 KB
testcase_26 AC 1,065 ms
34,988 KB
testcase_27 AC 1,069 ms
35,032 KB
testcase_28 AC 1,067 ms
34,940 KB
testcase_29 AC 1,066 ms
35,056 KB
testcase_30 AC 1,064 ms
34,956 KB
testcase_31 AC 48 ms
34,504 KB
testcase_32 AC 47 ms
34,452 KB
testcase_33 AC 46 ms
34,424 KB
testcase_34 AC 47 ms
34,508 KB
testcase_35 AC 47 ms
34,416 KB
testcase_36 AC 47 ms
34,376 KB
testcase_37 AC 53 ms
34,456 KB
testcase_38 AC 48 ms
34,384 KB
testcase_39 AC 47 ms
34,380 KB
testcase_40 AC 47 ms
34,392 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 2 ms
4,352 KB
testcase_43 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
constexpr ll MOD = 1000000007;
constexpr int inf = 1e9 + 10;
constexpr ll INF = (ll)4e18 + 10;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> G(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<int> s(n);
    vector<vector<ll>> dp(n, vector(n + 1, 0LL));
    auto dfs = [&](auto self, int v, int pre) -> void {
        dp[v][0] = 1;
        s[v] = 1;
        for (auto nv : G[v]) {
            if (nv == pre) continue;
            self(self, nv, v);
            vector ndp(n + 1, 0LL);
            rep(i, n - s[nv] + 1) {
                rep(j, s[nv] + 1) {
                    if (dp[v][i] and dp[nv][j])
                        ndp[i + j] = (ndp[i + j] + dp[v][i] * dp[nv][j] % MOD) % MOD;
                }
            }
            dp[v] = ndp;
            s[v] += s[nv];
        }
        dp[v][s[v]]++;
    };

    dfs(dfs, 0, -1);
    cout << dp[0][k] << endl;
    return 0;
}
0