結果

問題 No.196 典型DP (1)
ユーザー ei1333ei1333
提出日時 2015-07-08 17:41:23
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,097 bytes
コンパイル時間 1,306 ms
コンパイル使用メモリ 146,940 KB
実行使用メモリ 23,472 KB
最終ジャッジ日時 2023-09-22 09:36:26
合計ジャッジ時間 11,060 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
23,472 KB
testcase_01 AC 8 ms
19,336 KB
testcase_02 AC 7 ms
19,036 KB
testcase_03 AC 7 ms
19,076 KB
testcase_04 AC 7 ms
19,028 KB
testcase_05 AC 7 ms
19,012 KB
testcase_06 AC 8 ms
19,076 KB
testcase_07 AC 7 ms
19,060 KB
testcase_08 AC 7 ms
19,040 KB
testcase_09 AC 7 ms
19,208 KB
testcase_10 AC 7 ms
19,032 KB
testcase_11 AC 7 ms
19,320 KB
testcase_12 AC 8 ms
19,028 KB
testcase_13 AC 7 ms
19,032 KB
testcase_14 AC 8 ms
19,212 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 10 ms
19,184 KB
testcase_27 AC 12 ms
19,340 KB
testcase_28 AC 18 ms
19,308 KB
testcase_29 AC 25 ms
19,364 KB
testcase_30 AC 27 ms
19,364 KB
testcase_31 AC 8 ms
19,232 KB
testcase_32 AC 1,134 ms
19,128 KB
testcase_33 TLE -
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;

int N, K;
vector< int > graph[2000];
int dp[2000][2001];
int size[2000]; // SubTreeSize

int dfs(int idx, int prev)
{
  size[idx] = 1;
  for(int i = 0; i < graph[idx].size(); i++) {
    if(graph[idx][i] == prev) continue;
    size[idx] += dfs(graph[idx][i], idx);
  }
  return(size[idx]);
}


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

int main()
{ 
  fill_n(*dp, 2000 * 2001, -1);

  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);
  }
  dfs(0, -1);
  cout << rec(0, -1, K) << endl;
}
0