結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 3 ms
4,536 KB
testcase_16 AC 4 ms
7,348 KB
testcase_17 AC 6 ms
12,456 KB
testcase_18 AC 10 ms
20,148 KB
testcase_19 AC 11 ms
24,668 KB
testcase_20 AC 15 ms
34,888 KB
testcase_21 AC 16 ms
34,820 KB
testcase_22 AC 16 ms
34,928 KB
testcase_23 AC 18 ms
34,928 KB
testcase_24 AC 16 ms
34,800 KB
testcase_25 AC 16 ms
34,820 KB
testcase_26 AC 20 ms
34,896 KB
testcase_27 AC 21 ms
34,964 KB
testcase_28 AC 21 ms
34,836 KB
testcase_29 AC 19 ms
34,872 KB
testcase_30 AC 19 ms
35,116 KB
testcase_31 AC 19 ms
34,956 KB
testcase_32 AC 19 ms
34,936 KB
testcase_33 AC 19 ms
34,980 KB
testcase_34 AC 19 ms
34,820 KB
testcase_35 AC 19 ms
34,900 KB
testcase_36 AC 19 ms
34,908 KB
testcase_37 AC 16 ms
34,816 KB
testcase_38 AC 18 ms
34,940 KB
testcase_39 AC 18 ms
34,892 KB
testcase_40 AC 17 ms
34,904 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,380 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;

int main(){
   int N,K;
   scanf("%d%d",&N,&K);
   vector<int> edge[N];
   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;
   long long dp[N][N+1];
   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{
         long long tmp[2][N+1];
         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