結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-09-28 19:18:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,597 bytes |
| コンパイル時間 | 1,173 ms |
| コンパイル使用メモリ | 97,252 KB |
| 実行使用メモリ | 22,272 KB |
| 最終ジャッジ日時 | 2024-11-15 18:14:00 |
| 合計ジャッジ時間 | 19,560 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 TLE * 5 |
ソースコード
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void add(int &x, int v) {
x += v;
if (x >= MOD) x -= MOD;
}
int N, K;
vector<int> G[2000];
int sz[2000];
int dfs(int x, int p) {
sz[x] = 1;
for (int t : G[x]) {
if (t == p) continue;
sz[x] += dfs(t, x);
}
return sz[x];
}
int dp[2000][2001];
int dp2[2][2001];
void f(int x, int p) {
for (int t : G[x]) {
if (t == p) continue;
f(t, x);
}
int n = sz[x];
rep(i, n+1) dp2[0][i] = 0;
dp2[0][0] = 1;
for (int t : G[x]) {
if (t == p) continue;
rep(i, n+1) dp2[1][i] = 0;
for (int j=n; j>=0;j--){
rep(k, sz[t]+1) {
if (j+k > n) break;
add(dp2[1][j+k], (1LL*dp[t][k]*dp2[0][j])%MOD);
}
}
swap(dp2[0], dp2[1]);
}
assert(dp2[0][n] == 0);
dp2[0][n] = 1;
rep(i, n+1) dp[x][i] = dp2[0][i];
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
rep(i, N-1) {
int a, b;
cin >> a >> b;
G[a].pb(b);
G[b].pb(a);
}
dfs(0, -1);
f(0, -1);
cout << dp[0][K] << "\n";
return 0;
}