結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-12 11:27:17 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 110 ms / 2,000 ms |
| コード長 | 1,674 bytes |
| コンパイル時間 | 698 ms |
| コンパイル使用メモリ | 117,812 KB |
| 実行使用メモリ | 36,812 KB |
| 最終ジャッジ日時 | 2024-06-12 19:12:51 |
| 合計ジャッジ時間 | 3,319 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
immutable long MOD = 10^^9 + 7;
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto edges = new int[][](N);
foreach (i; 0..N-1) {
s = readln.split.map!(to!int);
edges[s[0]] ~= s[1];
edges[s[1]] ~= s[0];
}
if (K == 0) {
writeln(1);
return;
}
auto children = new int[](N);
auto dp = new long[][](N, N+2);
foreach (i; 0..N) fill(dp[i], 0);
auto tmp = new long[](N+1);
int dfs1(int n, int prev) {
children[n] = 1;
foreach (m; edges[n]) if (m != prev) children[n] += dfs1(m, n);
return children[n];
}
void dfs2(int n, int prev) {
foreach (m; edges[n]) if (m != prev) dfs2(m, n);
dp[n][children[n]] = 1;
if (children[n] == 1) return;
foreach (m; edges[n]) {
if (m == prev) continue;
fill(tmp, 0);
foreach (i; 0..children[n]) {
if (dp[n][i] == 0) continue;
foreach (j; 0..children[m]+1) {
if (i + j > children[n]) break;
tmp[i+j] += (dp[n][i] * dp[m][j]) % MOD;
tmp[i+j] %= MOD;
}
}
foreach (i; 0..children[n]+1) (dp[n][i] += tmp[i]) %= MOD;
foreach (i; 0..children[m]+1) (dp[n][i] += dp[m][i]) %= MOD;
}
}
dfs1(0, -1);
dfs2(0, -1);
//dp.each!writeln;
dp[0][K].writeln;
}