結果

問題 No.196 典型DP (1)
ユーザー Yang33Yang33
提出日時 2018-08-01 18:02:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,307 bytes
コンパイル時間 1,692 ms
コンパイル使用メモリ 171,364 KB
実行使用メモリ 35,396 KB
最終ジャッジ日時 2023-10-19 20:44:59
合計ジャッジ時間 3,375 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,720 KB
testcase_01 WA -
testcase_02 AC 3 ms
5,720 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,768 KB
testcase_07 WA -
testcase_08 AC 2 ms
5,796 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 3 ms
6,132 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 25 ms
35,360 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 25 ms
35,360 KB
testcase_31 AC 21 ms
34,680 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 21 ms
34,680 KB
testcase_36 AC 21 ms
33,328 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 20 ms
34,704 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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, 1, 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];
			dp[v][i] %=MOD;
			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