結果

問題 No.196 典型DP (1)
ユーザー 259_Momone259_Momone
提出日時 2019-02-26 11:23:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,190 bytes
コンパイル時間 1,259 ms
コンパイル使用メモリ 150,724 KB
実行使用メモリ 39,004 KB
最終ジャッジ日時 2023-09-02 14:26:36
合計ジャッジ時間 11,346 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 3 ms
4,380 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 3 ms
4,380 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long dynamic_programming(const long now, const long prev, vector<vector<long>>& dp, const vector<vector<long>>& edge){
    if(edge[now].size() == 1){
        dp[now][0] = dp[now][1] = 1;
        return 1;
    }
    auto tapi = [](vector<long>& to, const vector<long>& src) -> void{
        size_t N = to.size();
        vector<long> hoge(N);
        for(int i = 0; i < N; ++i)
            for(int j = 0; j <= i; ++j)hoge[i] += src[j] * to[i - j];
        copy(hoge.begin(), hoge.end(), to.begin());
    };
    long ret = 0;
    dp[now][0] = 1;
    for(const auto i : edge[now])
        if(i != prev) ret += dynamic_programming(i, now, dp, edge);
    for(const auto i : edge[now])
        if(i != prev) tapi(dp[now], dp[i]);
    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>(K + 1));
    dynamic_programming(0, 0, dp, edge);
    cout << dp[0][K] << endl;
}
0