結果

問題 No.196 典型DP (1)
ユーザー motimoti
提出日時 2015-07-15 19:21:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,121 bytes
コンパイル時間 1,133 ms
コンパイル使用メモリ 79,596 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 16:34:31
合計ジャッジ時間 2,935 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

using ll  = long long;
using VLL = vector<ll>;

int constexpr MOD = 1e9+7;
int constexpr Max = 2010;

int N, K;
VLL g[Max];

auto rec(int curr, int parent)
  -> VLL  // 着目している根付き木について、黒く塗られている数->そのパターン数
{
  VLL ret(1, 1);
  for(auto ch: g[curr]) {
    if(ch == parent) { continue; }
    auto subTree = rec(ch, curr);
    auto currK = ret.size() - 1; // 自分を除く
    auto newK = subTree.size() - 1; // 自分を除く
    VLL nret(currK + newK + 1);
    rep(iteCurrK, currK + 1) rep(iteNewK, newK + 1) {
      (nret[iteCurrK + iteNewK] += ret[iteCurrK] * subTree[iteNewK]) %= MOD;
    }
    ret = nret;
  }
  ret.push_back(1);
  return ret;
}

int main() {

  cin >> N >> K;
  rep(i, N-1) {
    int a, b; cin >> a >> b;
    g[a].push_back(b), g[b].push_back(a);
  }

  cout << rec(0, -1)[K] << endl;

  return 0;
}
0