結果

問題 No.196 典型DP (1)
ユーザー yaoshimaxyaoshimax
提出日時 2015-05-18 02:38:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,274 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 94,724 KB
実行使用メモリ 35,108 KB
最終ジャッジ日時 2023-09-20 09:42:05
合計ジャッジ時間 2,904 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
34,760 KB
testcase_01 AC 10 ms
34,864 KB
testcase_02 AC 10 ms
34,740 KB
testcase_03 AC 10 ms
34,804 KB
testcase_04 AC 11 ms
34,760 KB
testcase_05 AC 10 ms
34,744 KB
testcase_06 AC 10 ms
34,856 KB
testcase_07 AC 10 ms
34,808 KB
testcase_08 AC 10 ms
34,916 KB
testcase_09 AC 10 ms
35,028 KB
testcase_10 AC 10 ms
34,864 KB
testcase_11 AC 10 ms
34,744 KB
testcase_12 AC 10 ms
34,804 KB
testcase_13 AC 10 ms
34,796 KB
testcase_14 AC 10 ms
34,760 KB
testcase_15 AC 10 ms
34,840 KB
testcase_16 AC 11 ms
34,840 KB
testcase_17 AC 12 ms
34,844 KB
testcase_18 AC 13 ms
34,820 KB
testcase_19 AC 14 ms
35,108 KB
testcase_20 AC 15 ms
34,904 KB
testcase_21 AC 16 ms
35,100 KB
testcase_22 AC 15 ms
34,904 KB
testcase_23 AC 18 ms
34,840 KB
testcase_24 AC 16 ms
34,852 KB
testcase_25 AC 17 ms
34,852 KB
testcase_26 AC 20 ms
34,824 KB
testcase_27 AC 20 ms
35,036 KB
testcase_28 AC 20 ms
34,888 KB
testcase_29 AC 20 ms
34,956 KB
testcase_30 AC 20 ms
35,000 KB
testcase_31 AC 19 ms
34,960 KB
testcase_32 AC 19 ms
34,892 KB
testcase_33 AC 18 ms
34,832 KB
testcase_34 AC 19 ms
35,016 KB
testcase_35 AC 19 ms
34,972 KB
testcase_36 AC 18 ms
34,832 KB
testcase_37 AC 15 ms
35,000 KB
testcase_38 AC 18 ms
34,960 KB
testcase_39 AC 18 ms
34,892 KB
testcase_40 AC 18 ms
34,852 KB
testcase_41 AC 10 ms
34,924 KB
testcase_42 AC 10 ms
34,864 KB
testcase_43 AC 10 ms
34,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;
vector<int> edge[2000];
long long dp[2000][2001];
long long tmp[2][2001];

int main(){
   int N,K;
   scanf("%d%d",&N,&K);
   for( int i = 0 ; i <N-1; i++){
      int a,b;
      scanf("%d%d", &a,&b);
      edge[a].push_back(b);
      edge[b].push_back(a);
   }
   int parent[N];
   memset(parent,-1,sizeof(parent));
   queue<int> q;
   vector<int> order;
   parent[0]=0;
   q.push(0);
   while( !q.empty() ){
      int cur=q.front();
      //cout << cur<<endl;
      q.pop();
      order.push_back(cur);
      for( int i=0;i<(int)edge[cur].size();i++){
         if(parent[edge[cur][i]]<0){
            parent[edge[cur][i]]=cur;
            q.push(edge[cur][i]);
         } 
      }
   }
   int mod=1000000007;
   memset(dp,0,sizeof(dp));
   for( int i = N-1; i>=0; i-- ){
      if( edge[order[i]].size()==1&&edge[order[i]][0]==parent[order[i]] ){
         dp[order[i]][0]=1;
         dp[order[i]][1]=1;
      }
      else{
         memset(tmp,0,sizeof(tmp));
         tmp[0][0]=1;
         int cur=0;
         for( int j = 0 ; j < (int) edge[order[i]].size();j++){
            if(edge[order[i]][j]==parent[order[i]]) continue;
            memset(tmp[1-cur],0,sizeof(tmp[1-cur]));
            //cout << order[i]<<"->"<<edge[order[i]][j]<<endl;
            for( int k = 0 ; k <= N && dp[edge[order[i]][j]][k]>0; k++){
               //cout <<k<<":"<<dp[edge[order[i]][j]][k]<<endl;
               for( int l=0; l<=N&&tmp[cur][l]>0;l++){
                  tmp[1-cur][l+k]+=tmp[cur][l]*dp[edge[order[i]][j]][k];
                  tmp[1-cur][l+k]%=mod;
               }
            }
            cur=1-cur;
         }
         int j;
         for( j=0; j<=N&&tmp[cur][j]>0; j++){
            dp[order[i]][j]=tmp[cur][j];
            //printf("%d,%d:%d   ",order[i],j,(int)dp[order[i]][j]);
         }
         dp[order[i]][j]=1;
         //printf("\n");
      }
   }
   cout << dp[0][K]<<endl;
   return 0;
}
0