結果

問題 No.196 典型DP (1)
ユーザー cubinglovercubinglover
提出日時 2022-11-26 20:53:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,104 bytes
コンパイル時間 2,616 ms
コンパイル使用メモリ 206,196 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-04-14 06:51:19
合計ジャッジ時間 4,246 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 7 ms
6,944 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 10 ms
6,944 KB
testcase_21 AC 10 ms
6,940 KB
testcase_22 AC 10 ms
6,940 KB
testcase_23 AC 18 ms
10,240 KB
testcase_24 AC 15 ms
8,064 KB
testcase_25 AC 14 ms
7,552 KB
testcase_26 AC 28 ms
19,456 KB
testcase_27 AC 28 ms
19,584 KB
testcase_28 AC 28 ms
19,456 KB
testcase_29 AC 28 ms
19,584 KB
testcase_30 AC 27 ms
19,584 KB
testcase_31 AC 18 ms
6,940 KB
testcase_32 AC 17 ms
6,944 KB
testcase_33 AC 17 ms
6,940 KB
testcase_34 AC 17 ms
6,944 KB
testcase_35 AC 17 ms
6,944 KB
testcase_36 AC 17 ms
6,944 KB
testcase_37 AC 10 ms
6,940 KB
testcase_38 AC 18 ms
6,940 KB
testcase_39 AC 16 ms
6,944 KB
testcase_40 AC 17 ms
6,940 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (int i = (l); i < (int)(r); ++i)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
constexpr ll MOD = 1e9 + 7;
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};
const double PI = acos(-1);
template <typename T>
using MaxHeap = priority_queue<T>;
template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
inline bool chmax(T &a, T b) {
    return ((a < b) ? (a = b, true) : (false));
}
template <typename T>
inline bool chmin(T &a, T b) {
    return ((a > b) ? (a = b, true) : (false));
}
template <typename T>
inline void yn(const T &a) {
    ((a) ? (cout << "Yes" << endl) : (cout << "No" << endl));
}
template <typename T>
inline void YN(const T &a) {
    ((a) ? (cout << "YES" << endl) : (cout << "NO" << endl));
}
int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    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].emplace_back(b), G[b].emplace_back(a);
    }

    vector<vector<ll>> dp(n);

    auto f = [&G, &dp](auto self, const auto &v, const auto &p) -> int {
        int res = 1;
        dp[v].assign(res + 1, 0);
        dp[v][0] = 1;
        for (const auto &to : G[v]) {
            if (to == p) continue;
            res += self(self, to, v);
        }
        for (const auto &to : G[v]) {
            if (to == p) continue;
            vector<ll> merged(sz(dp[v]) + sz(dp[to]) - 1);
            rep(i, sz(dp[v])) rep(j, sz(dp[to])) {
                merged[i + j] += dp[v][i] * dp[to][j] % MOD;
                merged[i + j] %= MOD;
            }
            dp[v] = merged;
        }
        dp[v][res] = 1;
        return res;
    };

    f(f, 0, -1);

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

    return 0;
}
0