結果

問題 No.196 典型DP (1)
ユーザー 👑 kmjpkmjp
提出日時 2015-04-26 22:24:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 149,536 KB
実行使用メモリ 98,568 KB
最終ジャッジ日時 2023-09-18 12:00:44
合計ジャッジ時間 4,557 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,624 KB
testcase_12 AC 3 ms
4,748 KB
testcase_13 AC 3 ms
5,320 KB
testcase_14 AC 3 ms
5,640 KB
testcase_15 AC 7 ms
10,252 KB
testcase_16 AC 11 ms
16,188 KB
testcase_17 AC 17 ms
22,824 KB
testcase_18 AC 26 ms
29,160 KB
testcase_19 AC 30 ms
31,108 KB
testcase_20 AC 39 ms
35,768 KB
testcase_21 AC 39 ms
35,816 KB
testcase_22 AC 39 ms
35,912 KB
testcase_23 AC 80 ms
62,696 KB
testcase_24 AC 67 ms
52,628 KB
testcase_25 AC 64 ms
51,784 KB
testcase_26 AC 128 ms
98,516 KB
testcase_27 AC 129 ms
98,436 KB
testcase_28 AC 128 ms
98,484 KB
testcase_29 AC 128 ms
98,424 KB
testcase_30 AC 128 ms
98,568 KB
testcase_31 AC 80 ms
35,156 KB
testcase_32 AC 80 ms
35,152 KB
testcase_33 AC 80 ms
35,124 KB
testcase_34 AC 80 ms
35,464 KB
testcase_35 AC 80 ms
35,156 KB
testcase_36 AC 76 ms
35,156 KB
testcase_37 AC 40 ms
35,432 KB
testcase_38 AC 76 ms
35,172 KB
testcase_39 AC 67 ms
35,272 KB
testcase_40 AC 78 ms
35,188 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 1 ms
4,380 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[2020];
ll val[2020][2020];
ll mo=1000000007;
int num[2020];

void dfs(int cur,int pre) {
	num[cur]=1;
	ll nn[2][2020];
	int i,j,x;
	ZERO(nn);
	
	FOR(i,E[cur].size()) if(E[cur][i]==pre) break;
	if(i<E[cur].size()) E[cur].erase(E[cur].begin()+i);
	
	nn[0][0]=1;
	
	FOR(x,E[cur].size()) {
		int cu=x%2,ta=cu^1;
		int tar=E[cur][x];
		ZERO(nn[ta]);
		
		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