結果

問題 No.196 典型DP (1)
ユーザー pekempeypekempey
提出日時 2015-10-23 17:18:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 1,201 ms
コンパイル使用メモリ 146,780 KB
実行使用メモリ 34,588 KB
最終ジャッジ日時 2023-09-29 17:41:16
合計ジャッジ時間 3,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 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 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 4 ms
7,968 KB
testcase_16 AC 5 ms
14,212 KB
testcase_17 AC 7 ms
20,404 KB
testcase_18 AC 9 ms
26,620 KB
testcase_19 AC 10 ms
28,528 KB
testcase_20 AC 13 ms
32,968 KB
testcase_21 AC 13 ms
32,972 KB
testcase_22 AC 13 ms
32,964 KB
testcase_23 AC 14 ms
33,472 KB
testcase_24 AC 14 ms
33,276 KB
testcase_25 AC 14 ms
33,188 KB
testcase_26 AC 17 ms
34,588 KB
testcase_27 AC 17 ms
34,348 KB
testcase_28 AC 17 ms
34,308 KB
testcase_29 AC 16 ms
34,416 KB
testcase_30 AC 17 ms
34,508 KB
testcase_31 AC 18 ms
32,888 KB
testcase_32 AC 17 ms
32,888 KB
testcase_33 AC 18 ms
32,912 KB
testcase_34 AC 18 ms
32,884 KB
testcase_35 AC 18 ms
32,892 KB
testcase_36 AC 17 ms
32,944 KB
testcase_37 AC 13 ms
33,112 KB
testcase_38 AC 17 ms
32,888 KB
testcase_39 AC 16 ms
33,072 KB
testcase_40 AC 17 ms
32,808 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

vector<int> G[2020];
ll dp[2020][2020];
int size[2020];
const ll mod = 1e9 + 7;

int dfs2(int curr, int prev) {
	size[curr] = 1;
	for (int next : G[curr]) if (next != prev) {
		size[curr] += dfs2(next, curr);
	}
	return size[curr];
}

void dfs(int curr, int prev) {
	dp[curr][0] = 1;
	dp[curr][size[curr]] = 1;
	for (int next : G[curr]) if (next != prev) {
		dfs(next, curr);
		repr (i, size[curr]) if (dp[curr][i] > 0) rep (j, 1, size[next] + 1) {
			if (i + j <= size[curr])
				(dp[curr][i + j] += dp[curr][i] * dp[next][j]) %= mod;
		}
	}
}

int main() {
	int N, K;
	cin >> N >> K;
	rep (i, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs2(0, -1);
	dfs(0, -1);
	cout << dp[0][K] << endl;
	return 0;
}
0