結果

問題 No.196 典型DP (1)
ユーザー ctyl_0ctyl_0
提出日時 2015-10-05 19:48:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 91,272 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 07:04:02
合計ジャッジ時間 2,277 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define var auto
#define mod 1000000007
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

vector<vector<int>> ngb(2000); // neighbor

vector<ll> dnc(int n, int p){ // 親がpの頂点nを根にしたsubtreeで0~n個塗る場合の数を返す
    vector<ll> ret(1, 1); // 初期値: 0個塗る場合の数は1通り
    rep(i, ngb[n].size()){
        if (ngb[n][i] == p) { // 今いる頂点の親と子(実は親)が一緒の場合は何もしない
            continue;
        }
        
        // 分割統治
        vector<ll> add = dnc(ngb[n][i], n);
        vector<ll> new_ret(ret.size() + add.size() - 1, 0);
        
        rep(j, ret.size()){
            rep(k, add.size()){
                new_ret[j + k] = (new_ret[j + k] + ret[j] * add[k]) % mod;
            }
        }
        
        ret = new_ret;
    }
    ret.push_back(1); // 頂点nを塗る場合は全て塗るので1通り
    return ret;
}



int main() {
    
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    
    // source code
    int N = inputValue();
    int K = inputValue();
    rep(i, N - 1){
        int a = inputValue();
        int b = inputValue();
        ngb[a].push_back(b);
        ngb[b].push_back(a);
    }
    
    output(dnc(0, -1)[K], 0);
    
    return 0;
}
0