結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
cubinglover
|
| 提出日時 | 2022-11-26 20:58:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,296 bytes |
| コンパイル時間 | 2,149 ms |
| コンパイル使用メモリ | 205,740 KB |
| 最終ジャッジ日時 | 2025-02-09 01:27:25 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 TLE * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (int i = (l); i < (int)(r); ++i)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr int inf = 1e9 + 10;
constexpr ll INF = (ll)4e18 + 10;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const double PI = acos(-1);
template <typename T>
using MaxHeap = priority_queue<T>;
template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
inline bool chmax(T &a, T b) {
return ((a < b) ? (a = b, true) : (false));
}
template <typename T>
inline bool chmin(T &a, T b) {
return ((a > b) ? (a = b, true) : (false));
}
template <typename T>
inline void yn(const T &a) {
((a) ? (cout << "Yes" << endl) : (cout << "No" << endl));
}
template <typename T>
inline void YN(const T &a) {
((a) ? (cout << "YES" << endl) : (cout << "NO" << endl));
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<vector<int>> G(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
G[a].emplace_back(b), G[b].emplace_back(a);
}
vector dp(n, vector(2005, 0ll));
auto f = [&G, &dp](auto self, const auto &v, const auto &p) -> int {
dp[v][0] = 1;
int res = 1;
queue<int> que;
for (const auto &to : G[v]) {
if (to == p) continue;
int t = self(self, to, v);
que.push(t);
res += t;
}
int cnt = 0;
for (const auto &to : G[v]) {
if (to == p) continue;
int t = que.front();
que.pop();
for (int i = t + cnt + 1; i > 0; --i) {
ll x = 0;
rep(j, i + 1) {
if (i + j == 0) continue;
x += dp[v][i - j] * dp[to][j];
x %= MOD;
}
dp[v][i] = x;
}
cnt += t;
}
dp[v][res] = 1;
return res;
};
f(f, 0, -1);
cout << dp[0][k] << endl;
return 0;
}
cubinglover