結果

問題 No.196 典型DP (1)
ユーザー cormorancormoran
提出日時 2016-02-29 01:11:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 18:57:58
合計ジャッジ時間 2,169 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<tuple>
#include<numeric>
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()

template<typename T>
ostream& operator << (ostream &os , const vector<T> &v){
    rep(i,v.size()) os << v[i] << (i!=v.size()-1 ? " " : "\n"); return os;
}

template<typename T>
istream& operator >> (istream &is , vector<T> &v){
    rep(i,v.size()) is >> v[i]; return is;
}

#ifdef DEBUG
void debug(){ cerr << endl; }
#endif
template<class F,class...R>
void debug(const F &car,const R&... cdr){
#ifdef DEBUG
    cerr << car << " "; debug(cdr...);
#endif
}

using EdgeList = vector<vector<int>>;
const ll MOD = 1000000007;
EdgeList E;
int K;

vector<ll> convolution(const vector<ll> &A, const vector<ll> &B){
    int size = A.size() - 1 + B.size() - 1;
    vector<ll> ret( min(K+1, size + 1) );
    rep(i, A.size()){
        rep(j, B.size()){
            if(i + j > K) continue;
            ret[i+j] += (A[i] * B[j]) % MOD;
            ret[i+j] %= MOD;
        }
    }
    return ret;
}

vector<ll> dfs(int now, int pre){
    vector<ll> ret = {1};
    for(int nxt : E[now]){
        if(nxt == pre) continue;
        ret = convolution(ret, dfs(nxt, now));
    }
    if(ret.size() < K + 1) ret.push_back(1);
    debug("AT", now, ret);
    return ret;
}

bool solve(){
    int N; cin >> N >> K;
    E.resize(N);
    rep(i,N-1){
        int a, b; cin >> a >> b; 
        E[a].push_back(b);
        E[b].push_back(a);
    }
    vector<ll> ans = dfs(0, -1);
    cout << ( ans.size() > K ? ans[K] : 0 )<< endl;
    return false;
}

int main()
{
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}
0