結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
古寺いろは
|
| 提出日時 | 2015-04-26 22:58:46 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 35 ms / 2,000 ms |
| コード長 | 1,048 bytes |
| コンパイル時間 | 1,622 ms |
| コンパイル使用メモリ | 165,024 KB |
| 実行使用メモリ | 51,320 KB |
| 最終ジャッジ日時 | 2024-07-05 03:15:17 |
| 合計ジャッジ時間 | 3,346 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
vector<int> es[3000];
long long dp[3000][3000];
int child[3000];
long long mod = 1000000007;
int pos[3000];
int rev[3000];
int main() {
int N, K;
cin >> N >> K;
for (int i = 0; i < N - 1; i++)
{
int a, b;
cin >> a >> b;
es[a].push_back(b);
es[b].push_back(a);
}
queue<int> q;
q.push(0);
int cnt = 0;
for (int i = 0; i < N; i++)
{
pos[i] = rev[i] = -1;
}
pos[0] = 0;
rev[0] = 0;
cnt++;
while (!q.empty()){
int now = q.front(); q.pop();
for (auto i : es[now]){
if (pos[i] == -1){
pos[i] = cnt;
rev[cnt] = i;
cnt++;
q.push(i);
}
}
}
for (int ii = N - 1; ii >= 0; ii--)
{
int i = rev[ii];
child[i] = 1;
dp[i][0] = 1;
for (auto j : es[i]){
if (pos[j] < pos[i]) continue;
for (int k = child[i] - 1; k >= 0; k--)
{
for (int l = child[j]; l >= 1; l--)
{
dp[i][k + l] += dp[i][k] * dp[j][l];
dp[i][k + l] %= mod;
}
}
child[i] += child[j];
}
dp[i][child[i]] = 1;
}
cout << dp[0][K] % mod << endl;
}
古寺いろは