結果
問題 |
No.196 典型DP (1)
|
ユーザー |
![]() |
提出日時 | 2018-02-21 10:40:45 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,450 bytes |
コンパイル時間 | 1,441 ms |
コンパイル使用メモリ | 163,848 KB |
実行使用メモリ | 28,416 KB |
最終ジャッジ日時 | 2024-09-14 14:53:10 |
合計ジャッジ時間 | 10,474 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | AC * 26 TLE * 1 -- * 14 |
ソースコード
#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]; vector<ll> table(part[u]+1,0); rep(j,part[u]+1){ rep(l,part[v]+1){ if(j-l>=0) (table[j] += dp[v][l] * dp[u][j-l]) %= lmod; } } rep(j,part[u]+1) 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; }