結果

問題 No.196 典型DP (1)
ユーザー nanophoto12nanophoto12
提出日時 2021-11-23 16:11:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,555 bytes
コンパイル時間 4,277 ms
コンパイル使用メモリ 265,876 KB
実行使用メモリ 19,256 KB
最終ジャッジ日時 2023-09-07 18:52:26
合計ジャッジ時間 6,061 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 8 ms
4,860 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 13 ms
5,592 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 5 ms
5,700 KB
testcase_28 AC 20 ms
11,744 KB
testcase_29 AC 28 ms
16,796 KB
testcase_30 AC 31 ms
19,256 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 6 ms
4,380 KB
testcase_34 AC 9 ms
4,380 KB
testcase_35 AC 10 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 5 ms
4,384 KB
testcase_38 AC 6 ms
4,380 KB
testcase_39 AC 7 ms
4,376 KB
testcase_40 AC 10 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)

template<typename T>
static inline void chmin(T& ref, const T  value) {
	if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T& ref, const T value) {
	if (ref < value) ref = value;
}

#include <atcoder/all>
using namespace atcoder;
typedef modint1000000007 mint;


int main() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> g(n);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int> cs(n, 0);
	function<int(int, int)> dfs = [&](int current, int parent) {
		int c = 1;
		for (auto x : g[current]) {
			if (x == parent) continue;
			c += dfs(x, current);
		}
		cs[current] = c;
		return c;
	};
	dfs(0, -1);
	vector<mint> dp2(k + 1, 0);
	dp2[k] = 1;
	function<void(int, int, vector<mint>&)> dfs2 = [&](int current, int parent, vector<mint>& dp) {
		//塗らないパターン
		vector<mint> temp = dp;
		//子供を個別に塗り分けていくパターン
		for (auto x : g[current]) {
			if (x == parent) continue;
			dfs2(x, current, temp);
		}
		//自分以下をすべて塗るパターン
		for (int x = cs[current]; x <= k; x++) {
			temp[x - cs[current]] += dp[x];
		}
		dp = temp;
	};
	dfs2(0, -1, dp2);
	cout << dp2[0].val() << endl;
	return 0;
}
0