結果

問題 No.196 典型DP (1)
ユーザー 👑 kmjpkmjp
提出日時 2015-04-26 22:30:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 1,235 ms
コンパイル使用メモリ 151,360 KB
実行使用メモリ 96,216 KB
最終ジャッジ日時 2023-09-18 12:04:19
合計ジャッジ時間 4,758 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 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 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 3 ms
4,656 KB
testcase_13 AC 4 ms
4,920 KB
testcase_14 AC 4 ms
5,136 KB
testcase_15 AC 9 ms
9,684 KB
testcase_16 AC 15 ms
15,892 KB
testcase_17 AC 24 ms
22,052 KB
testcase_18 AC 36 ms
28,660 KB
testcase_19 AC 46 ms
30,616 KB
testcase_20 AC 52 ms
36,844 KB
testcase_21 AC 52 ms
36,484 KB
testcase_22 AC 54 ms
36,612 KB
testcase_23 AC 102 ms
62,688 KB
testcase_24 AC 87 ms
52,836 KB
testcase_25 AC 82 ms
51,572 KB
testcase_26 AC 135 ms
95,828 KB
testcase_27 AC 136 ms
96,216 KB
testcase_28 AC 137 ms
95,988 KB
testcase_29 AC 136 ms
96,124 KB
testcase_30 AC 136 ms
94,440 KB
testcase_31 AC 83 ms
36,364 KB
testcase_32 AC 83 ms
36,360 KB
testcase_33 AC 82 ms
36,328 KB
testcase_34 AC 82 ms
36,308 KB
testcase_35 AC 82 ms
36,312 KB
testcase_36 AC 79 ms
36,336 KB
testcase_37 AC 51 ms
36,476 KB
testcase_38 AC 79 ms
36,432 KB
testcase_39 AC 72 ms
36,420 KB
testcase_40 AC 81 ms
36,332 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
vector<int> E[2050];
ll val[2050][2050];
ll mo=1000000007;
int num[2050];

void dfs(int cur,int pre) {
	int i,j,x;
	vector<ll> nn[2];
	nn[0]=vector<ll>(2020,0);
	nn[1]=vector<ll>(2020,0);
	
	FOR(i,E[cur].size()) if(E[cur][i]==pre) break;
	if(i<E[cur].size()) E[cur].erase(E[cur].begin()+i);
	
	num[cur]=1;
	nn[0][0]=1;
	
	FOR(x,E[cur].size()) {
		int cu=x%2,ta=cu^1;
		int tar=E[cur][x];
		FOR(i,2020) nn[ta][i]=0;
		
		dfs(tar,cur);
		for(i=0;i<=num[tar];i++) if(val[tar][i]) {
			for(j=0;j<num[cur];j++) nn[ta][i+j] += nn[cu][j]*val[tar][i]%mo;
		}
		num[cur]+=num[tar];
		FOR(i,num[cur]+1) nn[ta][i] %= mo;
	}
	
	FOR(i,2010) val[cur][i]=nn[E[cur].size()%2][i];
	val[cur][num[cur]]=1;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	dfs(0,-1);
	cout<<val[0][K]<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0