結果

問題 No.196 典型DP (1)
ユーザー koba-e964koba-e964
提出日時 2021-08-17 14:30:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,523 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 81,248 KB
実行使用メモリ 35,500 KB
最終ジャッジ日時 2024-04-18 17:55:47
合計ジャッジ時間 2,550 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
35,280 KB
testcase_01 AC 10 ms
35,176 KB
testcase_02 AC 10 ms
35,216 KB
testcase_03 AC 10 ms
35,060 KB
testcase_04 AC 10 ms
35,184 KB
testcase_05 AC 10 ms
35,240 KB
testcase_06 AC 10 ms
35,108 KB
testcase_07 AC 10 ms
35,072 KB
testcase_08 AC 10 ms
35,116 KB
testcase_09 AC 11 ms
35,224 KB
testcase_10 AC 11 ms
35,252 KB
testcase_11 AC 10 ms
34,980 KB
testcase_12 AC 11 ms
35,248 KB
testcase_13 AC 10 ms
35,096 KB
testcase_14 AC 11 ms
35,144 KB
testcase_15 AC 11 ms
35,132 KB
testcase_16 AC 12 ms
35,236 KB
testcase_17 AC 12 ms
35,040 KB
testcase_18 AC 12 ms
35,252 KB
testcase_19 AC 14 ms
35,268 KB
testcase_20 AC 15 ms
35,352 KB
testcase_21 AC 15 ms
35,268 KB
testcase_22 AC 16 ms
35,316 KB
testcase_23 AC 16 ms
35,400 KB
testcase_24 AC 16 ms
35,296 KB
testcase_25 AC 17 ms
35,188 KB
testcase_26 AC 17 ms
35,472 KB
testcase_27 AC 18 ms
35,192 KB
testcase_28 AC 18 ms
35,424 KB
testcase_29 AC 17 ms
35,500 KB
testcase_30 AC 17 ms
35,436 KB
testcase_31 AC 21 ms
35,380 KB
testcase_32 AC 20 ms
35,100 KB
testcase_33 AC 19 ms
35,132 KB
testcase_34 AC 20 ms
35,300 KB
testcase_35 AC 20 ms
35,224 KB
testcase_36 AC 19 ms
35,260 KB
testcase_37 AC 15 ms
35,176 KB
testcase_38 AC 21 ms
35,048 KB
testcase_39 AC 20 ms
35,284 KB
testcase_40 AC 20 ms
35,304 KB
testcase_41 AC 10 ms
35,172 KB
testcase_42 AC 9 ms
35,256 KB
testcase_43 AC 11 ms
35,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
const int N = 2010;

const int DEBUG = 0;
VI graph[N];
int parent[N];
int desc[N];

ll dp[N][N];
const ll mod = 1e9+7;
void rec_p(int n, int p) {
  parent[n] = -2;
  int s = 0;
  REP(i, 0, graph[n].size()) {
    int v = graph[n][i];
    if (parent[v] != -2) {
      rec_p(v, n);
      s += desc[v] + 1;
    }
  }
  parent[n] = p;
  desc[n] = s;
}
// [0,d]
void poly_mul(ll aa[N], const ll bb[N], int da, int db) {
  ll cc[N] = {0};
  REP(i, 0, da + 1) {
    REP(j, 0, db + 1) {
      cc[i + j] += aa[i] * bb[j];
      cc[i + j] %= mod;
    }
  }
  REP(i, 0, da + db + 1) {
    if (DEBUG) {
      cout << cc[i] << "x^" << i << "+";
    }

    aa[i] = cc[i];
  }
  if (DEBUG) {
    cout << endl;
  }
}

void rec(int v) {
  dp[v][0] = 1;
  int d = 0;
  REP(i, 0, graph[v].size()) {
    int u = graph[v][i];
    if (u == parent[v]) {
      continue;
    }
    rec(u);
    poly_mul(dp[v], dp[u], d, desc[u] + 1);
    d += desc[u] + 1;
  }
  dp[v][desc[v] + 1] = 1;
}

int main(void){
  int n, k;
  cin >> n >> k;
  REP (i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  rec_p(0, -1);
  if (DEBUG) {
    REP(i, 0, n) {
      cout << "p[" << i << "]=" << parent[i] << endl;
    }
  }
  REP(i,0,N) {
    fill_n(dp[i], N, 0);
    dp[i][0] = 1;
  }
  rec(0);
  cout << dp[0][k] << endl;
}
0