結果

問題 No.196 典型DP (1)
コンテスト
ユーザー sorachandu
提出日時 2026-05-27 01:49:30
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,127 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,759 ms
コンパイル使用メモリ 340,396 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-27 01:49:41
合計ジャッジ時間 4,835 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/modint>
using mint=atcoder::modint1000000007;
std::ostream &operator<< (std::ostream &os, mint m){
    os<<m.val();
    return os;
}

int main(){
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    int N,K;
    cin>>N>>K;
    vector G(N,vector<int>());
    for(int i=0;i<N-1;i++){
        int a,b;
        cin>>a>>b;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    auto dfs=[&G](this auto dfs,int p,int par=-1) -> vector<array<mint,2>> {
        vector<array<mint,2>> dp1(2);
        dp1[0][0]=dp1[1][1]=1;
        for(auto q:G[p]) if(q!=par){
            auto dp2=dfs(q,p);
            const int A=ssize(dp1);
            const int B=ssize(dp2);
            vector<array<mint,2>> ndp(A+B-1);
            for(int i=0;i<A;i++){
                for(int j=0;j<B;j++){
                    ndp[i+j][0]+=dp1[i][0]*(dp2[j][0]+dp2[j][1]);
                    ndp[i+j][1]+=dp1[i][1]*dp2[j][1];
                }
            }
            swap(dp1,ndp);
        }
        return dp1;
    };
    auto res=dfs(0);
    cout<<res[K][0]+res[K][1]<<"\n";
}
0