結果

問題 No.196 典型DP (1)
ユーザー ctyl_0ctyl_0
提出日時 2015-10-05 19:43:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 588 ms / 2,000 ms
コード長 1,794 bytes
コンパイル時間 856 ms
コンパイル使用メモリ 94,452 KB
実行使用メモリ 222,740 KB
最終ジャッジ日時 2023-09-27 07:03:04
合計ジャッジ時間 14,367 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 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 2 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,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 AC 63 ms
4,376 KB
testcase_17 AC 145 ms
4,520 KB
testcase_18 AC 265 ms
5,716 KB
testcase_19 AC 337 ms
6,236 KB
testcase_20 AC 496 ms
5,724 KB
testcase_21 AC 502 ms
6,164 KB
testcase_22 AC 497 ms
6,276 KB
testcase_23 AC 548 ms
98,764 KB
testcase_24 AC 540 ms
64,092 KB
testcase_25 AC 538 ms
60,112 KB
testcase_26 AC 588 ms
222,724 KB
testcase_27 AC 585 ms
222,732 KB
testcase_28 AC 579 ms
222,740 KB
testcase_29 AC 583 ms
222,720 KB
testcase_30 AC 574 ms
222,680 KB
testcase_31 AC 437 ms
4,376 KB
testcase_32 AC 437 ms
4,376 KB
testcase_33 AC 438 ms
4,376 KB
testcase_34 AC 440 ms
4,380 KB
testcase_35 AC 438 ms
4,380 KB
testcase_36 AC 461 ms
4,376 KB
testcase_37 AC 503 ms
4,660 KB
testcase_38 AC 463 ms
4,380 KB
testcase_39 AC 470 ms
4,376 KB
testcase_40 AC 449 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 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<ll> dnc(vector<vector<int>> ngb, 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, 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() {
    
    // source code
    int N = inputValue();
    int K = inputValue();
    vector<vector<int>> ngb(N); // neighbor
    rep(i, N - 1){
        int a = inputValue();
        int b = inputValue();
        ngb[a].push_back(b);
        ngb[b].push_back(a);
    }
    
    var ans = dnc(ngb, 0, -1);
    output(dnc(ngb, 0, -1)[K], 0);
    
    return 0;
}
0