結果

問題 No.196 典型DP (1)
ユーザー kazumakazuma
提出日時 2018-11-27 13:20:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,990 bytes
コンパイル時間 3,075 ms
コンパイル使用メモリ 167,604 KB
実行使用メモリ 19,576 KB
最終ジャッジ日時 2023-09-05 13:01:36
合計ジャッジ時間 3,722 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,204 KB
testcase_01 AC 7 ms
19,032 KB
testcase_02 AC 7 ms
19,092 KB
testcase_03 AC 7 ms
19,020 KB
testcase_04 AC 7 ms
19,280 KB
testcase_05 AC 7 ms
19,024 KB
testcase_06 AC 7 ms
19,144 KB
testcase_07 AC 7 ms
19,040 KB
testcase_08 AC 7 ms
19,168 KB
testcase_09 AC 7 ms
19,044 KB
testcase_10 AC 7 ms
19,028 KB
testcase_11 AC 7 ms
19,028 KB
testcase_12 AC 7 ms
19,104 KB
testcase_13 AC 7 ms
19,124 KB
testcase_14 AC 7 ms
19,092 KB
testcase_15 AC 8 ms
19,036 KB
testcase_16 AC 8 ms
19,072 KB
testcase_17 AC 9 ms
19,128 KB
testcase_18 AC 10 ms
19,212 KB
testcase_19 AC 11 ms
19,152 KB
testcase_20 AC 13 ms
19,084 KB
testcase_21 AC 13 ms
19,076 KB
testcase_22 AC 13 ms
19,148 KB
testcase_23 AC 14 ms
19,244 KB
testcase_24 AC 13 ms
19,156 KB
testcase_25 AC 13 ms
19,180 KB
testcase_26 AC 13 ms
19,368 KB
testcase_27 AC 14 ms
19,332 KB
testcase_28 AC 13 ms
19,372 KB
testcase_29 AC 13 ms
19,576 KB
testcase_30 AC 13 ms
19,436 KB
testcase_31 AC 18 ms
19,088 KB
testcase_32 AC 18 ms
19,180 KB
testcase_33 AC 18 ms
19,364 KB
testcase_34 AC 19 ms
19,176 KB
testcase_35 AC 19 ms
19,248 KB
testcase_36 AC 18 ms
19,300 KB
testcase_37 AC 13 ms
19,240 KB
testcase_38 AC 18 ms
19,088 KB
testcase_39 AC 17 ms
19,244 KB
testcase_40 AC 18 ms
19,184 KB
testcase_41 AC 7 ms
19,024 KB
testcase_42 AC 7 ms
19,164 KB
testcase_43 AC 7 ms
19,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<int MOD>
class mod_int {
	unsigned x;
public:
	mod_int() : x(0) { }
	mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	mod_int &operator/=(mod_int that) { return *this *= that.inverse(); }

	mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
	mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
	mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
	mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }

	bool operator==(const mod_int& that) const { return x == that.x; }

	mod_int inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		return mod_int(u);
	}
};

const int mod = 1e9 + 7;
using mint = mod_int<mod>;

const int MAX = 2000;

vector<int> G[MAX];

int size[MAX];
mint dp[MAX][MAX + 1];
mint tmp[MAX + 1];

void dfs(int v, int prev) {
	size[v] = 0;
	dp[v][0] = 1;
	for (auto to : G[v]) if (to != prev) {
		dfs(to, v);
		for (int j = 0; j <= size[v]; j++) {
			tmp[j] = dp[v][j];
			dp[v][j] = 0;
		}
		for (int j = size[v]; j >= 0; j--) {
			for (int k = 0; k <= size[to]; k++) {
				dp[v][j + k] += tmp[j] * dp[to][k];
			}
		}
		size[v] += size[to];
	}
	size[v] += 1;
	dp[v][size[v]] += 1;
}

int main()
{
	int N, K;
	cin >> N >> K;
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(0, -1);
	cout << dp[0][K].get() << endl;
	return 0;
}
0