結果

問題 No.196 典型DP (1)
ユーザー 193s193s
提出日時 2017-09-28 19:47:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,861 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 98,452 KB
実行使用メモリ 19,436 KB
最終ジャッジ日時 2024-04-27 15:10:30
合計ジャッジ時間 2,417 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 3 ms
7,732 KB
testcase_16 AC 5 ms
9,912 KB
testcase_17 AC 7 ms
12,108 KB
testcase_18 AC 11 ms
15,836 KB
testcase_19 AC 12 ms
16,740 KB
testcase_20 AC 17 ms
17,772 KB
testcase_21 AC 17 ms
17,528 KB
testcase_22 AC 18 ms
17,528 KB
testcase_23 AC 18 ms
18,976 KB
testcase_24 AC 17 ms
18,824 KB
testcase_25 AC 17 ms
18,700 KB
testcase_26 AC 18 ms
19,296 KB
testcase_27 AC 18 ms
19,300 KB
testcase_28 AC 18 ms
19,300 KB
testcase_29 AC 19 ms
19,060 KB
testcase_30 AC 18 ms
19,436 KB
testcase_31 AC 20 ms
17,648 KB
testcase_32 AC 22 ms
18,676 KB
testcase_33 AC 23 ms
17,664 KB
testcase_34 AC 22 ms
18,548 KB
testcase_35 AC 22 ms
18,672 KB
testcase_36 AC 20 ms
18,544 KB
testcase_37 AC 17 ms
18,684 KB
testcase_38 AC 22 ms
17,540 KB
testcase_39 AC 20 ms
17,676 KB
testcase_40 AC 22 ms
17,656 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void add(int &x, int v) {
  x += v;
  if (x >= MOD) x -= MOD;
}

int N, K;
vector<int> G[2000];

int sz[2000];
int dfs(int x, int p) {
  sz[x] = 1;
  for (int t : G[x]) {
    if (t == p) continue;
    sz[x] += dfs(t, x);
  }
  return sz[x];
}

int dp[2000][2001];
int dp2[2][2001];
void f(int x, int p) {
  for (int t : G[x]) {
    if (t == p) continue;
    f(t, x);
  }

  rep(i, N+1) dp2[0][i] = 0;
  dp2[0][0] = 1;
  /*
  int n = 1;
  for (int t : G[x]) {
    if (t == p) continue;
    n += sz[t];
    rep(i, n+1) dp2[1][i] = 0;
    rep(j, n-sz[t]+1) {
      rep(k, sz[t]+1) {
        add(dp2[1][j+k], (1LL*dp[t][k]*dp2[0][j])%MOD);
      }
    }
    swap(dp2[0], dp2[1]);
  }
  */

  int n = sz[x];
  for (int t : G[x]) {
    if (t == p) continue;
    rep(i, N+1) dp2[1][i] = 0;
    rep(j, N+1) {
      if (dp2[0][j] == 0) continue;
      rep(k, sz[t]+1) {
        add(dp2[1][j+k], (1LL*dp[t][k]*dp2[0][j])%MOD);
      }
    }
    swap(dp2[0], dp2[1]);
  }
  assert(dp2[0][n] == 0);
  dp2[0][n] = 1;
  rep(i, n+1) dp[x][i] = dp2[0][i];
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> K;
  rep(i, N-1) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs(0, -1);
  f(0, -1);
  cout << dp[0][K] << "\n";
  return 0;
}
0