結果

問題 No.196 典型DP (1)
ユーザー ei1333333ei1333333
提出日時 2015-09-09 19:45:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 147,012 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2023-09-26 10:16:19
合計ジャッジ時間 3,573 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 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,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
6,484 KB
testcase_16 AC 4 ms
8,612 KB
testcase_17 AC 7 ms
12,668 KB
testcase_18 AC 10 ms
14,848 KB
testcase_19 AC 11 ms
16,824 KB
testcase_20 AC 15 ms
17,448 KB
testcase_21 AC 14 ms
17,468 KB
testcase_22 AC 14 ms
17,576 KB
testcase_23 AC 18 ms
18,208 KB
testcase_24 AC 17 ms
18,108 KB
testcase_25 AC 18 ms
17,820 KB
testcase_26 AC 24 ms
18,992 KB
testcase_27 AC 25 ms
19,072 KB
testcase_28 AC 24 ms
18,800 KB
testcase_29 AC 25 ms
19,044 KB
testcase_30 AC 25 ms
18,776 KB
testcase_31 AC 16 ms
17,444 KB
testcase_32 AC 17 ms
17,520 KB
testcase_33 AC 16 ms
17,640 KB
testcase_34 AC 17 ms
17,444 KB
testcase_35 AC 17 ms
17,524 KB
testcase_36 AC 16 ms
17,508 KB
testcase_37 AC 15 ms
17,468 KB
testcase_38 AC 16 ms
17,448 KB
testcase_39 AC 16 ms
17,500 KB
testcase_40 AC 17 ms
17,472 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int N, K;
vector< int > graph[2000];

int dp[2000][2001];
int num[2000];
int rec(int idx, int prev)
{
  for(int i = 0; i < graph[idx].size(); i++) {
    if(graph[idx][i] != prev) rec(graph[idx][i], idx);
  }
  dp[idx][0] = 1;
  num[idx] = 1;
  for(int i = 0; i < graph[idx].size(); i++) {
    if(graph[idx][i] == prev) continue;
    for(int j = num[idx]; j >= 0; j--) {
      for(int k = num[graph[idx][i]]; k >= 1; k--) {
        (dp[idx][j + k] += (long long)dp[idx][j] * dp[graph[idx][i]][k] % mod) %= mod;
      }
    }
    num[idx] += num[graph[idx][i]];
  }
  dp[idx][num[idx]] = 1;
  return(dp[idx][K]);
}

int main()
{
  cin >> N >> K;
  for(int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  cout << rec(0, -1) << endl;
}
0