結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-12 11:12:25 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,523 bytes |
| コンパイル時間 | 851 ms |
| コンパイル使用メモリ | 116,248 KB |
| 実行使用メモリ | 49,024 KB |
| 最終ジャッジ日時 | 2024-06-12 19:11:58 |
| 合計ジャッジ時間 | 9,737 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 11 WA * 15 TLE * 1 -- * 14 |
ソースコード
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];
}
auto children = new int[](N);
auto dp = new long[][](N, N+2);
foreach (i; 0..N) fill(dp[i], 0);
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;
auto tmp = new long[](children[n]+1);
fill(tmp, 0);
foreach (i; 0..children[n]) {
foreach (j; 0..children[m]+1) {
if (i + j > children[n]) break;
tmp[i+j] += dp[n][i] * dp[m][j];
}
}
foreach (i; 0..children[n]+1) dp[n][i] += tmp[i];
foreach (i; 0..children[m]+1) dp[n][i] += dp[m][i];
}
}
dfs1(0, -1);
dfs2(0, -1);
//dp.each!writeln;
dp[0][K].writeln;
}