結果

問題 No.196 典型DP (1)
ユーザー 259_Momone259_Momone
提出日時 2019-02-26 11:54:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,330 bytes
コンパイル時間 1,713 ms
コンパイル使用メモリ 173,104 KB
実行使用メモリ 34,276 KB
最終ジャッジ日時 2023-09-02 18:25:14
合計ジャッジ時間 3,905 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 1 ms
4,368 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,368 KB
testcase_17 AC 4 ms
4,368 KB
testcase_18 AC 5 ms
4,368 KB
testcase_19 AC 5 ms
4,372 KB
testcase_20 AC 4 ms
4,368 KB
testcase_21 AC 4 ms
4,368 KB
testcase_22 AC 4 ms
4,372 KB
testcase_23 AC 10 ms
8,208 KB
testcase_24 AC 4 ms
4,444 KB
testcase_25 AC 14 ms
9,392 KB
testcase_26 AC 3 ms
4,368 KB
testcase_27 AC 9 ms
8,084 KB
testcase_28 AC 22 ms
20,528 KB
testcase_29 AC 32 ms
30,176 KB
testcase_30 AC 35 ms
34,276 KB
testcase_31 AC 3 ms
4,372 KB
testcase_32 AC 12 ms
4,372 KB
testcase_33 AC 16 ms
4,368 KB
testcase_34 AC 20 ms
4,368 KB
testcase_35 AC 21 ms
4,368 KB
testcase_36 AC 3 ms
4,368 KB
testcase_37 AC 6 ms
4,368 KB
testcase_38 AC 16 ms
4,368 KB
testcase_39 AC 15 ms
4,368 KB
testcase_40 AC 21 ms
4,372 KB
testcase_41 AC 2 ms
4,372 KB
testcase_42 AC 2 ms
4,368 KB
testcase_43 AC 1 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

long dynamic_programming(const unsigned long K, const long now, const long prev, vector<vector<long>>& dp, const vector<vector<long>>& edge){
    auto tapi = [&K](vector<long>& to, const vector<long>& src) -> void{
        size_t N = min(to.size() + src.size() - 1, K + 1);
        vector<long> hoge(N);
        for(int i = 0; i < to.size(); ++i)
            if(to[i])
                for(int j = 0; j < src.size(); ++j){
                    if(i + j > K)break;
                    hoge[i + j] += to[i] * src[j] % 1000000007;
                }
        to = vector<long>(N);
        transform(hoge.begin(), hoge.end(), to.begin(), [](auto i){return i % 1000000007;});
    };
    long ret = 0;
    dp[now][0] = 1;
    for(const auto i : edge[now])
        if(i != prev){
            ret += dynamic_programming(K, i, now, dp, edge);
            tapi(dp[now], dp[i]);
        }
    if(ret + 1 <= K)dp[now].push_back(1);
    return ret + 1;
}

int main(){
    unsigned long N, K;
    cin >> N >> K;

    vector<vector<long>> edge(N);
    for(int i = 1, a, b; i < N; ++i){
        cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }

    vector<vector<long>> dp(N, vector<long>(1, 1));
    dynamic_programming(K, 0, 0, dp, edge);
    cout << dp[0][K] << endl;
}
0