結果

問題 No.196 典型DP (1)
ユーザー toshiki_fulltoshiki_full
提出日時 2019-03-01 01:17:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 938 bytes
コンパイル時間 1,755 ms
コンパイル使用メモリ 175,408 KB
実行使用メモリ 19,216 KB
最終ジャッジ日時 2023-09-05 16:25:51
合計ジャッジ時間 3,535 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 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 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 4 ms
5,160 KB
testcase_17 AC 6 ms
7,584 KB
testcase_18 AC 10 ms
11,484 KB
testcase_19 AC 13 ms
13,916 KB
testcase_20 AC 18 ms
18,836 KB
testcase_21 AC 18 ms
18,872 KB
testcase_22 AC 18 ms
18,828 KB
testcase_23 AC 18 ms
18,940 KB
testcase_24 AC 18 ms
18,936 KB
testcase_25 AC 19 ms
18,936 KB
testcase_26 AC 19 ms
19,132 KB
testcase_27 AC 19 ms
19,208 KB
testcase_28 AC 19 ms
19,216 KB
testcase_29 AC 19 ms
19,128 KB
testcase_30 AC 18 ms
19,152 KB
testcase_31 AC 20 ms
18,944 KB
testcase_32 AC 20 ms
18,892 KB
testcase_33 AC 20 ms
18,940 KB
testcase_34 AC 20 ms
18,940 KB
testcase_35 AC 20 ms
18,940 KB
testcase_36 AC 20 ms
18,880 KB
testcase_37 AC 18 ms
18,816 KB
testcase_38 AC 20 ms
18,816 KB
testcase_39 AC 19 ms
18,872 KB
testcase_40 AC 21 ms
18,952 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}
inline int mul(int a, int b) {
  return (int)((long long)a * b % md);
}
vector<vector<int>> grh, dp;
vector<int> dsc;
void dfs (int crr, int prt) {
  dp[crr][0] = 1;
  for (int nxt : grh[crr]) if (nxt != prt) {
    dfs(nxt, crr);
    for (int i = dsc[crr]; i >= 0; i--) {
      for (int j = 1; j <= dsc[nxt]; j++) {
        add(dp[crr][i + j], mul(dp[crr][i], dp[nxt][j]));
      }
    }
    dsc[crr] += dsc[nxt];
  }
  dsc[crr]++;
  dp[crr][dsc[crr]] = 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  grh.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    grh[a].push_back(b);
    grh[b].push_back(a);
  }
  dp.resize(n, vector<int>(n + 1, 0));
  dsc.resize(n, 0);
  dfs(0, 0);
  cout << dp[0][k] << '\n';
  return 0;
}
0