結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,720 KB
testcase_01 AC 2 ms
5,720 KB
testcase_02 AC 3 ms
5,720 KB
testcase_03 AC 2 ms
5,740 KB
testcase_04 AC 2 ms
5,752 KB
testcase_05 AC 3 ms
5,760 KB
testcase_06 AC 3 ms
5,768 KB
testcase_07 AC 3 ms
5,796 KB
testcase_08 AC 2 ms
5,796 KB
testcase_09 AC 2 ms
5,800 KB
testcase_10 AC 3 ms
5,932 KB
testcase_11 AC 3 ms
6,132 KB
testcase_12 AC 3 ms
8,180 KB
testcase_13 AC 3 ms
8,204 KB
testcase_14 AC 3 ms
8,288 KB
testcase_15 AC 4 ms
12,264 KB
testcase_16 AC 7 ms
18,460 KB
testcase_17 AC 9 ms
22,580 KB
testcase_18 AC 12 ms
28,800 KB
testcase_19 AC 14 ms
32,860 KB
testcase_20 AC 16 ms
34,948 KB
testcase_21 AC 16 ms
34,892 KB
testcase_22 AC 16 ms
35,028 KB
testcase_23 AC 19 ms
35,076 KB
testcase_24 AC 18 ms
35,040 KB
testcase_25 AC 18 ms
35,048 KB
testcase_26 AC 23 ms
35,360 KB
testcase_27 AC 22 ms
35,360 KB
testcase_28 AC 23 ms
35,360 KB
testcase_29 AC 22 ms
35,360 KB
testcase_30 AC 23 ms
35,360 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 18 ms
34,680 KB
testcase_35 AC 19 ms
34,680 KB
testcase_36 AC 19 ms
34,732 KB
testcase_37 AC 16 ms
34,948 KB
testcase_38 AC 19 ms
34,692 KB
testcase_39 AC 18 ms
34,760 KB
testcase_40 AC 19 ms
34,704 KB
testcase_41 AC 2 ms
5,764 KB
testcase_42 AC 2 ms
5,764 KB
testcase_43 AC 2 ms
5,764 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 のどの子もまた黒で塗られる。

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

2011年ぐらいの有名なアレ?

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

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;
			}
		}
		FOR(i, 0, 2002) {
			dp[v][i] = temp[i];
			temp[i] = 0;
		}
		sz[v] += sz[nx];
	}
	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