結果

問題 No.196 典型DP (1)
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-26 22:58:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,048 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 150,716 KB
実行使用メモリ 49,668 KB
最終ジャッジ日時 2023-09-18 12:22:56
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
5,936 KB
testcase_15 AC 5 ms
11,992 KB
testcase_16 AC 8 ms
20,204 KB
testcase_17 AC 13 ms
28,568 KB
testcase_18 AC 20 ms
36,636 KB
testcase_19 AC 25 ms
40,784 KB
testcase_20 AC 33 ms
49,116 KB
testcase_21 AC 32 ms
48,948 KB
testcase_22 AC 33 ms
48,900 KB
testcase_23 AC 34 ms
49,292 KB
testcase_24 AC 33 ms
49,056 KB
testcase_25 AC 33 ms
49,080 KB
testcase_26 AC 35 ms
49,660 KB
testcase_27 AC 34 ms
49,588 KB
testcase_28 AC 34 ms
49,536 KB
testcase_29 AC 34 ms
49,632 KB
testcase_30 AC 35 ms
49,668 KB
testcase_31 AC 34 ms
49,008 KB
testcase_32 AC 34 ms
49,000 KB
testcase_33 AC 34 ms
48,944 KB
testcase_34 AC 34 ms
49,012 KB
testcase_35 AC 34 ms
49,228 KB
testcase_36 AC 34 ms
48,948 KB
testcase_37 AC 33 ms
48,948 KB
testcase_38 AC 34 ms
49,008 KB
testcase_39 AC 34 ms
49,000 KB
testcase_40 AC 34 ms
49,260 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

vector<int> es[3000];
long long dp[3000][3000];
int child[3000];

long long mod = 1000000007;

int pos[3000];
int rev[3000];

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

	queue<int> q;
	q.push(0);
	int cnt = 0;
	for (int i = 0; i < N; i++)
	{
		pos[i] = rev[i] = -1;
	}
	pos[0] = 0;
	rev[0] = 0;
	cnt++;
	while (!q.empty()){
		int now = q.front(); q.pop();
		for (auto i : es[now]){
			if (pos[i] == -1){
				pos[i] = cnt;
				rev[cnt] = i;
				cnt++;
				q.push(i);
			}
		}
	}
	for (int ii = N - 1; ii >= 0; ii--)
	{
		int i = rev[ii];
		child[i] = 1;
		dp[i][0] = 1;
		for (auto j : es[i]){
			if (pos[j] < pos[i]) continue;
			for (int k = child[i] - 1; k >= 0; k--)
			{
				for (int l = child[j]; l >= 1; l--)
				{
					dp[i][k + l] += dp[i][k] * dp[j][l];
					dp[i][k + l] %= mod;
				}
			}
			child[i] += child[j];
		}
		dp[i][child[i]] = 1;
	}


	cout << dp[0][K] % mod << endl;
}
0