結果

問題 No.196 典型DP (1)
ユーザー chocoruskchocorusk
提出日時 2018-12-13 19:55:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 99,324 KB
実行使用メモリ 66,256 KB
最終ジャッジ日時 2023-10-25 09:31:14
合計ジャッジ時間 5,056 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
66,052 KB
testcase_01 AC 52 ms
66,052 KB
testcase_02 AC 43 ms
66,040 KB
testcase_03 AC 52 ms
66,052 KB
testcase_04 AC 53 ms
66,052 KB
testcase_05 AC 52 ms
66,052 KB
testcase_06 AC 43 ms
66,040 KB
testcase_07 AC 52 ms
66,052 KB
testcase_08 AC 52 ms
66,052 KB
testcase_09 AC 52 ms
66,052 KB
testcase_10 AC 52 ms
66,056 KB
testcase_11 AC 52 ms
66,056 KB
testcase_12 AC 52 ms
66,056 KB
testcase_13 AC 52 ms
66,056 KB
testcase_14 AC 52 ms
66,056 KB
testcase_15 AC 53 ms
66,064 KB
testcase_16 AC 55 ms
66,080 KB
testcase_17 AC 56 ms
66,100 KB
testcase_18 AC 61 ms
66,112 KB
testcase_19 AC 59 ms
66,120 KB
testcase_20 AC 55 ms
66,132 KB
testcase_21 AC 55 ms
66,136 KB
testcase_22 AC 54 ms
66,132 KB
testcase_23 AC 58 ms
66,184 KB
testcase_24 AC 55 ms
66,164 KB
testcase_25 AC 67 ms
66,164 KB
testcase_26 AC 54 ms
66,256 KB
testcase_27 AC 57 ms
66,256 KB
testcase_28 AC 65 ms
66,256 KB
testcase_29 AC 73 ms
66,256 KB
testcase_30 AC 44 ms
66,104 KB
testcase_31 AC 54 ms
66,144 KB
testcase_32 AC 62 ms
66,144 KB
testcase_33 AC 66 ms
66,144 KB
testcase_34 AC 75 ms
66,144 KB
testcase_35 AC 43 ms
66,116 KB
testcase_36 AC 53 ms
66,144 KB
testcase_37 AC 61 ms
66,136 KB
testcase_38 AC 66 ms
66,144 KB
testcase_39 AC 68 ms
66,144 KB
testcase_40 AC 43 ms
66,116 KB
testcase_41 AC 52 ms
66,052 KB
testcase_42 AC 51 ms
66,052 KB
testcase_43 AC 52 ms
66,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
vector<int> g[2000];
bool used[2000];
int num[2000], c[4000];
int s[2000];
int ct;
int n;
void dfs(int x){
	s[x]=1;
	num[x]=ct;
	c[ct]=x;
	ct++;
	used[x]=1;
	for(auto y:g[x]){
		if(!used[y]){
			dfs(y);
			s[x]+=s[y];
		}
	}
	c[ct]=x+n;
	ct++;
}

int main()
{
	int k;
	cin>>n>>k;
	for(int i=0; i<n-1; i++){
		int a, b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	if(k==n){
		cout<<1<<endl;
		return 0;
	}
	dfs(0);
	ll dp[4001][2001]={};
	dp[0][0]=1;
	for(int i=1; i<2*n; i++){
		for(int j=0; j<=k; j++){
			dp[i][j]+=dp[i-1][j];
			if(c[i]>n && j>=s[c[i]-n]) dp[i][j]+=dp[num[c[i]-n]-1][j-s[c[i]-n]];
			dp[i][j]%=MOD;
		}
	}
	cout<<dp[2*n-1][k]<<endl;
	return 0;
}
0