結果

問題 No.196 典型DP (1)
ユーザー kurarrrkurarrr
提出日時 2018-02-21 10:57:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 1,290 ms
コンパイル使用メモリ 150,552 KB
実行使用メモリ 35,200 KB
最終ジャッジ日時 2023-10-12 16:36:13
合計ジャッジ時間 3,700 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 4 ms
8,292 KB
testcase_16 AC 6 ms
14,672 KB
testcase_17 AC 8 ms
20,940 KB
testcase_18 AC 11 ms
27,376 KB
testcase_19 AC 12 ms
29,300 KB
testcase_20 AC 13 ms
32,880 KB
testcase_21 AC 13 ms
33,108 KB
testcase_22 AC 13 ms
32,936 KB
testcase_23 AC 16 ms
33,712 KB
testcase_24 AC 15 ms
33,204 KB
testcase_25 AC 17 ms
34,348 KB
testcase_26 AC 18 ms
34,360 KB
testcase_27 AC 18 ms
34,336 KB
testcase_28 AC 19 ms
34,676 KB
testcase_29 AC 20 ms
35,068 KB
testcase_30 AC 21 ms
35,200 KB
testcase_31 AC 22 ms
32,864 KB
testcase_32 AC 24 ms
33,572 KB
testcase_33 AC 25 ms
34,200 KB
testcase_34 AC 25 ms
34,768 KB
testcase_35 AC 26 ms
34,832 KB
testcase_36 AC 21 ms
33,112 KB
testcase_37 AC 14 ms
33,624 KB
testcase_38 AC 24 ms
34,124 KB
testcase_39 AC 22 ms
34,532 KB
testcase_40 AC 25 ms
34,868 KB
testcase_41 AC 2 ms
4,352 KB
testcase_42 AC 1 ms
4,352 KB
testcase_43 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back

template<class T> void chmax(T& a,const T& b){a=max(a,b);}
template<class T> void chmin(T& a,const T& b){a=min(a,b);}

using namespace std;

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll lmod = 1e9+7,LINF=1LL<<60;
const int MAX_N = 2003;

ll dp[MAX_N][MAX_N];
vector<int> graph[MAX_N];
int part[MAX_N];

int main(){
  int N,K;
  cin >> N >> K;
  rep(i,N-1){
    int a,b; cin >> a >> b;
    graph[a].pb(b); graph[b].pb(a);
  }
  rep(i,N) rep(j,K+1) dp[i][j] = 0LL;
  rep(i,N) dp[i][0] = 1LL;
  fill(part,part+N,1);
  function<void(int,int)> dfs = [&](int u,int p){
    for(auto v:graph[u]){
      if(v==p) continue;
      dfs(v,u);
      part[u] += part[v];
      // 元々の部分木 -> part[u]-part[v]-1
      // 新しい部分木 -> part[v]
      vector<ll> table(part[u]+1,0);
      rep(j,part[u]-part[v]){
        rep(l,part[v]+1){
          (table[j+l] += dp[u][j] * dp[v][l]) %= lmod;
        }
      }
      rep(j,part[u]) dp[u][j] = table[j];
    }
    dp[u][part[u]] = 1LL;
    return ;
  };
  dfs(0,-1);
  cout << dp[0][K] << endl;
  // rep(i,N){ rep(j,K+1) cout << dp[i][j] << " "; cout << endl;}
  return 0;
}
0