結果

問題 No.196 典型DP (1)
ユーザー startcppstartcpp
提出日時 2018-03-07 21:40:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,716 bytes
コンパイル時間 598 ms
コンパイル使用メモリ 61,656 KB
実行使用メモリ 65,752 KB
最終ジャッジ日時 2024-04-15 03:09:15
合計ジャッジ時間 2,735 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
9,628 KB
testcase_16 AC 7 ms
15,784 KB
testcase_17 AC 14 ms
21,948 KB
testcase_18 AC 19 ms
28,244 KB
testcase_19 AC 23 ms
30,296 KB
testcase_20 AC 31 ms
34,800 KB
testcase_21 AC 31 ms
34,680 KB
testcase_22 AC 30 ms
34,660 KB
testcase_23 AC 34 ms
35,108 KB
testcase_24 AC 33 ms
34,952 KB
testcase_25 AC 32 ms
34,692 KB
testcase_26 AC 33 ms
35,796 KB
testcase_27 AC 33 ms
35,404 KB
testcase_28 AC 33 ms
35,156 KB
testcase_29 AC 32 ms
35,012 KB
testcase_30 AC 34 ms
35,156 KB
testcase_31 AC 66 ms
64,816 KB
testcase_32 AC 63 ms
65,752 KB
testcase_33 AC 64 ms
65,252 KB
testcase_34 AC 63 ms
64,984 KB
testcase_35 AC 63 ms
65,460 KB
testcase_36 AC 65 ms
62,692 KB
testcase_37 AC 34 ms
36,880 KB
testcase_38 AC 62 ms
62,932 KB
testcase_39 AC 54 ms
58,632 KB
testcase_40 AC 63 ms
64,880 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//与えられた木から部分木を0個以上カットしてできる大きさN-Kの木は何通り?という問題.
#include <iostream>
#include <vector>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int mod = 1000000007;
int N, K, u, v;
vector<int> et[2000];
int tsz[2000];			//tsz[i]    = 頂点i以下の頂点の個数
int tdp[2000][2000];	//tdp[i][j] = 頂点i以下でj頂点残す方法の数
int cdp[2000][2000];	//cdp[i][j] = 子i個で合計j頂点残す方法の数
bool solvedTdp[2000];

int dfs(int p, int v) {
	int i, ret = 1;
	rep(i, et[v].size()) {
		if (et[v][i] == p) continue;
		ret += dfs(v, et[v][i]);
	}
	return tsz[v] = ret;
}

void dfs2(int p, int v) {
	int i, j, k;
	
	vector<int> childs;
	rep(i, et[v].size()) {
		if (et[v][i] == p) continue;
		if (!solvedTdp[et[v][i]]) dfs2(v, et[v][i]);
		childs.push_back(et[v][i]);
	}
	
	if (childs.size() == 0) {	//葉の場合
		tdp[v][0] = tdp[v][1] = 1;
		solvedTdp[v] = true;
		return;
	}
	
	rep(i, childs.size() + 1) rep(j, tsz[v]) cdp[i][j] = 0;
	cdp[0][0] = 1;
	
	int chnum = 0;
	rep(i, childs.size()) {	//今まで子をi個見た
		rep(j, chnum + 1) {	//今までj頂点残した
			rep(k, tsz[childs[i]] + 1) {	//i番目の子でk頂点残す
				cdp[i + 1][j + k] += cdp[i][j] * tdp[childs[i]][k];
				cdp[i + 1][j + k] %= mod;
			}
		}
		chnum += tsz[childs[i]];
	}
	
	rep(i, tsz[v] + 1) {
		if (i == 0) tdp[v][0] = 1;
		else tdp[v][i] = cdp[childs.size()][i - 1];
	}
	solvedTdp[v] = true;
	return;
}

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