結果

問題 No.196 典型DP (1)
ユーザー Yang33Yang33
提出日時 2018-08-01 18:12:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,254 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 171,424 KB
実行使用メモリ 35,184 KB
最終ジャッジ日時 2023-10-19 20:45:13
合計ジャッジ時間 3,284 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,700 KB
testcase_01 AC 2 ms
5,700 KB
testcase_02 AC 2 ms
5,700 KB
testcase_03 AC 3 ms
5,704 KB
testcase_04 AC 3 ms
5,712 KB
testcase_05 AC 3 ms
5,720 KB
testcase_06 AC 2 ms
5,720 KB
testcase_07 AC 2 ms
5,728 KB
testcase_08 AC 2 ms
5,728 KB
testcase_09 AC 2 ms
5,732 KB
testcase_10 AC 2 ms
5,784 KB
testcase_11 AC 3 ms
5,868 KB
testcase_12 AC 3 ms
7,916 KB
testcase_13 AC 3 ms
7,924 KB
testcase_14 AC 3 ms
7,924 KB
testcase_15 AC 4 ms
12,032 KB
testcase_16 AC 6 ms
18,204 KB
testcase_17 AC 8 ms
22,332 KB
testcase_18 AC 10 ms
28,504 KB
testcase_19 AC 11 ms
32,604 KB
testcase_20 AC 13 ms
34,676 KB
testcase_21 AC 13 ms
34,676 KB
testcase_22 AC 13 ms
34,676 KB
testcase_23 AC 16 ms
34,840 KB
testcase_24 AC 15 ms
34,812 KB
testcase_25 AC 15 ms
34,808 KB
testcase_26 AC 20 ms
35,172 KB
testcase_27 AC 20 ms
35,180 KB
testcase_28 AC 20 ms
35,184 KB
testcase_29 AC 20 ms
35,176 KB
testcase_30 AC 20 ms
35,152 KB
testcase_31 AC 19 ms
34,680 KB
testcase_32 AC 19 ms
34,680 KB
testcase_33 AC 19 ms
34,680 KB
testcase_34 AC 19 ms
34,680 KB
testcase_35 AC 19 ms
34,680 KB
testcase_36 AC 18 ms
34,680 KB
testcase_37 AC 13 ms
34,680 KB
testcase_38 AC 17 ms
34,680 KB
testcase_39 AC 17 ms
34,680 KB
testcase_40 AC 19 ms
34,680 KB
testcase_41 AC 3 ms
5,724 KB
testcase_42 AC 2 ms
5,724 KB
testcase_43 AC 2 ms
5,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/07/31  Problem: yukicoder 196  / Link: http://yukicoder.me/problems/no/196  ----- */
/* ------問題------

N 個の頂点をもつ根付き木と整数 K が与えられます。この木の各頂点を白または黒で塗ることを考えます。
次の2条件をともに満たすような塗り方の個数を 1,000,000,007 で割った余りを求めてください。
1. ちょうど K 個の頂点が黒で塗られる。
2. 黒で塗られる任意の頂点 v について、v のどの子もまた黒で塗られる。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL dp[2002][2002];
int sz[2002];
LL temp[2002];
int K;
void f(int v, int p, const VVI&G) {
	sz[v] = 1;

	dp[v][0] = 1;
	for (int nx : G[v]) {
		if (nx == p)continue;
		f(nx, v, G);
		FOR(rk, 0, sz[v] + 1) {
			FOR(k, 0, sz[nx] + 1) {
				if (rk + k == sz[v] + sz[nx])continue;
				temp[k+rk] += dp[v][rk]*dp[nx][k];
				temp[k + rk] %= MOD;
			}
		}
		sz[v] += sz[nx];
		FOR(i, 0, sz[v]+1) {
			dp[v][i] = temp[i];
			temp[i] = 0;
		}
	}
	dp[v][sz[v]]++;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N; cin >> N >> K;
	VVI G(N);
	FOR(i, 0, N - 1) {
		int a, b; cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	f(0, -1, G);

	LL ans = 0;
	ans = dp[0][K];


	cout << ans << "\n";

	return 0;
}
0