結果
問題 | No.196 典型DP (1) |
ユーザー |
![]() |
提出日時 | 2015-07-15 19:21:59 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 11 ms / 2,000 ms |
コード長 | 1,121 bytes |
コンパイル時間 | 608 ms |
コンパイル使用メモリ | 84,864 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-08 08:05:40 |
合計ジャッジ時間 | 1,877 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <complex> #include <queue> #include <set> #include <map> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) using ll = long long; using VLL = vector<ll>; int constexpr MOD = 1e9+7; int constexpr Max = 2010; int N, K; VLL g[Max]; auto rec(int curr, int parent) -> VLL // 着目している根付き木について、黒く塗られている数->そのパターン数 { VLL ret(1, 1); for(auto ch: g[curr]) { if(ch == parent) { continue; } auto subTree = rec(ch, curr); auto currK = ret.size() - 1; // 自分を除く auto newK = subTree.size() - 1; // 自分を除く VLL nret(currK + newK + 1); rep(iteCurrK, currK + 1) rep(iteNewK, newK + 1) { (nret[iteCurrK + iteNewK] += ret[iteCurrK] * subTree[iteNewK]) %= MOD; } ret = nret; } ret.push_back(1); return ret; } int main() { cin >> N >> K; rep(i, N-1) { int a, b; cin >> a >> b; g[a].push_back(b), g[b].push_back(a); } cout << rec(0, -1)[K] << endl; return 0; }