結果

問題 No.196 典型DP (1)
ユーザー Div9851Div9851
提出日時 2017-10-08 00:29:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,197 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 161,536 KB
実行使用メモリ 96,988 KB
最終ジャッジ日時 2024-04-28 12:56:54
合計ジャッジ時間 2,843 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 4 ms
8,644 KB
testcase_16 AC 5 ms
14,608 KB
testcase_17 AC 8 ms
20,864 KB
testcase_18 AC 11 ms
27,048 KB
testcase_19 AC 12 ms
29,500 KB
testcase_20 AC 13 ms
33,440 KB
testcase_21 AC 13 ms
33,608 KB
testcase_22 AC 14 ms
33,676 KB
testcase_23 AC 36 ms
61,764 KB
testcase_24 AC 26 ms
50,540 KB
testcase_25 AC 28 ms
49,292 KB
testcase_26 AC 61 ms
96,972 KB
testcase_27 AC 62 ms
96,976 KB
testcase_28 AC 64 ms
96,720 KB
testcase_29 AC 64 ms
96,964 KB
testcase_30 AC 67 ms
96,988 KB
testcase_31 AC 18 ms
33,056 KB
testcase_32 AC 23 ms
32,940 KB
testcase_33 AC 25 ms
33,040 KB
testcase_34 AC 25 ms
33,044 KB
testcase_35 AC 24 ms
33,040 KB
testcase_36 AC 17 ms
32,944 KB
testcase_37 AC 14 ms
33,188 KB
testcase_38 AC 23 ms
32,952 KB
testcase_39 AC 22 ms
33,112 KB
testcase_40 AC 24 ms
32,928 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define MP(a,b) make_pair(a,b)
typedef long long LL;
typedef pair<int, int> PI;
typedef vector<int> VI;
const LL MOD = 1000000007LL;
vector<int> G[2000];
LL dp[2000][2001];
int tree[2000];
int N, K;
void cnt(int v, int par) {
	tree[v] = 1;
	rep(i, G[v].size()) {
		int to = G[v][i];
		if (to == par) continue;
		cnt(to, v);
		tree[v] += tree[to];
	}
}
void dfs(int v, int par) {
	LL dp2[2][2001] = { 0 };
	int sum = 0;
	int now = 0;
	dp2[now][0] = 1;
	rep(i, G[v].size()) {
		int to = G[v][i];
		if (to == par) continue;
		dfs(to, v);
		for (int j = 0; j <= sum; j++) {
			for (int k = 0; k <= tree[to]; k++) {
				LL t = (dp2[now][j] * dp[to][k]) % MOD;
				if (j + k <= K) (dp2[now^1][j + k] += t) %= MOD;
			}
		}
		for (int j = 0; j <= tree[v]; j++) dp2[now][j] = 0;
		sum += tree[to];
		now ^= 1;
	}
	(dp2[now][tree[v]] += 1) %= MOD;
	for (int i = 0; i <= tree[v]; i++) dp[v][i] = dp2[now][i];
}
int main() {
	cin >> N >> K;
	rep(i, N - 1) {
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	cnt(0, -1);
	dfs(0, -1);
	cout << dp[0][K] << endl;

}
0