結果

問題 No.196 典型DP (1)
ユーザー 259_Momone259_Momone
提出日時 2019-02-26 11:38:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 1,420 ms
コンパイル使用メモリ 152,308 KB
実行使用メモリ 27,072 KB
最終ジャッジ日時 2023-09-02 15:30:05
合計ジャッジ時間 3,718 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 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 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 9 ms
7,196 KB
testcase_24 AC 4 ms
4,412 KB
testcase_25 AC 11 ms
7,532 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 8 ms
7,684 KB
testcase_28 AC 21 ms
17,408 KB
testcase_29 AC 29 ms
24,784 KB
testcase_30 AC 31 ms
27,072 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 12 ms
4,500 KB
testcase_33 AC 17 ms
4,376 KB
testcase_34 AC 22 ms
4,376 KB
testcase_35 AC 22 ms
4,376 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 6 ms
4,376 KB
testcase_38 AC 16 ms
4,380 KB
testcase_39 AC 14 ms
4,376 KB
testcase_40 AC 22 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,380 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){
    if(edge[now].size() == 1 && edge[now][0] == prev){
        dp[now].push_back(1);
        return 1;
    }
    auto tapi = [&K](vector<long>& to, const vector<long>& src) -> void{
        size_t N = min(to.size() + src.size(), K + 1);
        vector<long> hoge(N);
        for(int i = 0; i < to.size(); ++i){
            if(!to[i])continue;
            for(int j = 0; j < src.size(); ++j){
                if(i + j > K)break;
                hoge[i + j] += to[i] * src[j] % 1000000007;
            }
        }
        for(auto& i : hoge)i %= 1000000007;
        to = vector<long>(hoge);
    };
    long ret = 0;
    dp[now][0] = 1;
    for(const auto i : edge[now])
        if(i != prev) ret += dynamic_programming(K, i, now, dp, edge);
    for(const auto i : edge[now])
        if(i != prev){
            tapi(dp[now], dp[i]);
        }

    if(ret + 1 <= K)dp[now].resize(ret + 2);
    if(ret + 1 < dp[now].size())dp[now][ret + 1] = 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