結果

問題 No.196 典型DP (1)
ユーザー beetbeet
提出日時 2018-06-21 19:47:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 173,652 KB
実行使用メモリ 34,260 KB
最終ジャッジ日時 2023-09-13 07:37:33
合計ジャッジ時間 3,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 7 ms
4,376 KB
testcase_23 AC 17 ms
14,104 KB
testcase_24 AC 13 ms
11,092 KB
testcase_25 AC 12 ms
9,672 KB
testcase_26 AC 31 ms
33,972 KB
testcase_27 AC 32 ms
34,084 KB
testcase_28 AC 32 ms
34,260 KB
testcase_29 AC 31 ms
33,936 KB
testcase_30 AC 31 ms
33,968 KB
testcase_31 AC 15 ms
4,376 KB
testcase_32 AC 14 ms
4,380 KB
testcase_33 AC 15 ms
4,380 KB
testcase_34 AC 14 ms
4,376 KB
testcase_35 AC 15 ms
4,380 KB
testcase_36 AC 14 ms
4,380 KB
testcase_37 AC 7 ms
4,376 KB
testcase_38 AC 14 ms
4,376 KB
testcase_39 AC 12 ms
4,376 KB
testcase_40 AC 14 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  Int n,k;
  cin>>n>>k;
  vector<vector<Int> > G(n);
  for(Int i=1;i<n;i++){
    Int x,y;
    cin>>x>>y;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
  }
  const Int MOD = 1e9+7;
  vector<vector<Int> > dp(n);
  vector<Int> sz(n,1);
  function<void(Int,Int)> dfs=
    [&](Int v,Int p){
      for(Int u:G[v]){
	if(u==p) continue;
	dfs(u,v);
	sz[v]+=sz[u];
      }
      dp[v].assign(1,1);
      for(Int u:G[v]){
	if(u==p) continue;
	vector<Int> nx(dp[v].size()+sz[u],0);
	for(Int i=0;i<(Int)dp[v].size();i++)
	  for(Int j=0;j<=sz[u];j++)
	    nx[i+j]+=dp[v][i]*dp[u][j]%MOD;
	for(Int i=0;i<(Int)nx.size();i++) nx[i]%=MOD;
	dp[v]=move(nx);
      }
      dp[v].emplace_back(1);
    };
  dfs(0,-1);
  cout<<dp[0][k]<<endl;
  return 0;
}
0