結果

問題 No.196 典型DP (1)
ユーザー latte0119latte0119
提出日時 2016-02-22 22:45:03
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,330 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 162,468 KB
実行使用メモリ 39,644 KB
最終ジャッジ日時 2024-09-22 13:07:21
合計ジャッジ時間 2,787 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   49 |     scanf("%lld%lld",&N,&K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:52:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |         scanf("%lld%lld",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

const int mod=1000000007;

int N,K;
vint G[2222];
int dp[2222][2222];
int sz[2222];

int tmp[2222];
void dfs(int v,int p){
    dp[v][0]=1;
    sz[v]=0;
    rep(hoge,G[v].size()){
        int to=G[v][hoge];
        if(to==p)continue;
        dfs(to,v);
        memset(tmp,0,sizeof(tmp));
        for(int i=0;i<=sz[v];i++){
            for(int j=0;j<=sz[to];j++){
                tmp[i+j]=(tmp[i+j]+dp[v][i]*dp[to][j])%mod;
            }
        }
        sz[v]+=sz[to];
        for(int i=0;i<=sz[v];i++)dp[v][i]=tmp[i];
    }
    dp[v][sz[v]+1]=dp[v][sz[v]];
    sz[v]++;
}

signed main(){
    scanf("%lld%lld",&N,&K);
    rep(i,N-1){
        int a,b;
        scanf("%lld%lld",&a,&b);
        G[a].pb(b);G[b].pb(a);
    }

    dfs(0,-1);
    printf("%lld\n",dp[0][K]);
    return 0;
}
0