結果

問題 No.196 典型DP (1)
ユーザー piyoko_212piyoko_212
提出日時 2015-12-28 18:58:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 894 bytes
コンパイル時間 1,229 ms
コンパイル使用メモリ 42,288 KB
実行使用メモリ 70,568 KB
最終ジャッジ日時 2023-10-19 11:50:02
合計ジャッジ時間 3,247 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,408 KB
testcase_01 AC 2 ms
5,408 KB
testcase_02 AC 2 ms
5,408 KB
testcase_03 AC 2 ms
5,408 KB
testcase_04 AC 2 ms
5,408 KB
testcase_05 AC 2 ms
5,412 KB
testcase_06 AC 2 ms
5,412 KB
testcase_07 AC 3 ms
5,412 KB
testcase_08 AC 2 ms
5,416 KB
testcase_09 AC 3 ms
5,412 KB
testcase_10 AC 2 ms
5,420 KB
testcase_11 AC 2 ms
5,432 KB
testcase_12 AC 2 ms
5,432 KB
testcase_13 AC 2 ms
7,484 KB
testcase_14 AC 3 ms
7,472 KB
testcase_15 AC 5 ms
11,608 KB
testcase_16 AC 8 ms
17,792 KB
testcase_17 AC 13 ms
23,956 KB
testcase_18 AC 19 ms
30,148 KB
testcase_19 AC 23 ms
32,196 KB
testcase_20 AC 32 ms
38,348 KB
testcase_21 AC 31 ms
38,392 KB
testcase_22 AC 31 ms
38,376 KB
testcase_23 AC 34 ms
38,368 KB
testcase_24 AC 34 ms
38,336 KB
testcase_25 AC 33 ms
38,344 KB
testcase_26 AC 33 ms
38,476 KB
testcase_27 AC 34 ms
38,476 KB
testcase_28 AC 34 ms
38,476 KB
testcase_29 AC 34 ms
38,476 KB
testcase_30 AC 34 ms
38,476 KB
testcase_31 AC 66 ms
70,460 KB
testcase_32 AC 66 ms
70,568 KB
testcase_33 AC 65 ms
70,568 KB
testcase_34 AC 64 ms
70,568 KB
testcase_35 AC 65 ms
70,568 KB
testcase_36 AC 62 ms
67,492 KB
testcase_37 AC 33 ms
40,544 KB
testcase_38 AC 62 ms
67,428 KB
testcase_39 AC 55 ms
63,000 KB
testcase_40 AC 63 ms
69,680 KB
testcase_41 AC 2 ms
5,412 KB
testcase_42 AC 2 ms
5,412 KB
testcase_43 AC 2 ms
5,412 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:36:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |         int a,b;scanf("%d%d",&a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:39:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |                 scanf("%d%d",&p,&q);
      |                 ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>g[2100];
int sz[2100];
int mod=1000000007;
long long dp[2100][2100];
long long dp2[2100][2100];
void dfs(int a,int b){
	sz[a]=1;
	for(int i=0;i<g[a].size();i++){
		if(b!=g[a][i]){
			dfs(g[a][i],a);
			sz[a]+=sz[g[a][i]];
		}
	}
	int now=0;
	dp2[0][0]=1;
	int cnt=0;
	for(int i=0;i<g[a].size();i++){
		if(b==g[a][i])continue;
		for(int j=0;j<=now+sz[g[a][i]];j++)dp2[cnt+1][j]=0;
		for(int j=0;j<=now;j++){
			for(int k=0;k<=sz[g[a][i]];k++){
				dp2[cnt+1][j+k]=(dp2[cnt+1][j+k]+dp2[cnt][j]*dp[g[a][i]][k])%mod;
			}
		}
		cnt++;
		now+=sz[g[a][i]];
	}
	for(int i=0;i<sz[a];i++)dp[a][i]=dp2[cnt][i];
	dp[a][sz[a]]++;
}
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<a-1;i++){
		int p,q;
		scanf("%d%d",&p,&q);
		g[p].push_back(q);
		g[q].push_back(p);
	}
	dfs(0,-1);
	printf("%lld\n",dp[0][b]);
}
0