結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 4 ms
5,356 KB
testcase_17 AC 6 ms
8,256 KB
testcase_18 AC 11 ms
11,876 KB
testcase_19 AC 13 ms
14,136 KB
testcase_20 AC 18 ms
19,236 KB
testcase_21 AC 18 ms
19,344 KB
testcase_22 AC 19 ms
19,240 KB
testcase_23 AC 20 ms
19,340 KB
testcase_24 AC 19 ms
19,300 KB
testcase_25 AC 20 ms
19,288 KB
testcase_26 AC 21 ms
19,536 KB
testcase_27 AC 21 ms
19,480 KB
testcase_28 AC 21 ms
19,536 KB
testcase_29 AC 21 ms
19,472 KB
testcase_30 AC 21 ms
19,596 KB
testcase_31 AC 26 ms
19,236 KB
testcase_32 AC 26 ms
19,300 KB
testcase_33 AC 26 ms
19,356 KB
testcase_34 AC 27 ms
19,240 KB
testcase_35 AC 27 ms
19,368 KB
testcase_36 AC 26 ms
19,252 KB
testcase_37 AC 19 ms
19,216 KB
testcase_38 AC 25 ms
19,256 KB
testcase_39 AC 25 ms
19,256 KB
testcase_40 AC 26 ms
19,240 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 <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) {
  for (int nxt : grh[crr]) {
    if (nxt != prt) {
      dfs(nxt, crr);
      vector<int> tmp(dsc[crr] + dsc[nxt] + 1, 0);
      for (int i = dsc[crr]; i >= 0; i--) {
        for (int j = 0; j <= dsc[nxt]; j++) {
          int &crrv = tmp[i + j];
          add(crrv, mul(dp[crr][i], dp[nxt][j]));
        }
      }
      for (int i = 0; i <= dsc[crr] + dsc[nxt]; i++) {
        dp[crr][i] = tmp[i];
      }
      dsc[crr] += dsc[nxt];
    }
  }
  dsc[crr]++;
}
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, 1));
  dsc.resize(n, 0);
  dfs(0, 0);
  cout << dp[0][k] << '\n';
  return 0;
}
0