結果

問題 No.196 典型DP (1)
ユーザー Kana NagataKana Nagata
提出日時 2019-05-17 00:17:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 1,726 ms
コンパイル使用メモリ 174,860 KB
実行使用メモリ 18,688 KB
最終ジャッジ日時 2023-10-17 07:06:21
合計ジャッジ時間 4,672 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
constexpr int md = 1e9 + 7;
inline void mad (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);
}
auto convolution (
    vector<int> a,
    vector<int> b
  ) -> vector<int>
  {
    int l = a.size();
    int m = b.size();
    int n = l + m - 1;
    assert(l && m);
    vector<int> c(n, 0);
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < m; j++) {
        mad(c[i + j], mul(a[i], b[j]));
      }
    }
    return c;
  }
void dfs (
  vector<vector<int>>& grh,
  vector<int>& sz,
  vector<vector<int>>& dp,
  int crr = 0,
  int prt = 0
) {
  for (const int nxt : grh[crr]) if (nxt != prt) {
    dfs(grh, sz, dp, nxt, crr);
    sz[crr] += sz[nxt];
  }
  dp[crr] = {1};
  for (const int nxt : grh[crr]) if (nxt != prt) {
    dp[crr] = convolution(dp[crr], dp[nxt]);
  }
  dp[crr].push_back(1);
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> grh(n);
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    grh[s].push_back(t);
    grh[t].push_back(s);
  }
  vector<int> sz(n, 1);
  vector<vector<int>> dp(n);
  dfs(grh, sz, dp);
  cout << dp[0][k] << endl;
  return 0;
}
0