結果

問題 No.196 典型DP (1)
ユーザー yumakmcyumakmc
提出日時 2016-02-21 21:34:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 3,488 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 180,340 KB
実行使用メモリ 12,712 KB
最終ジャッジ日時 2023-10-23 18:49:49
合計ジャッジ時間 8,188 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,620 KB
testcase_01 AC 5 ms
11,620 KB
testcase_02 AC 5 ms
11,620 KB
testcase_03 AC 5 ms
11,620 KB
testcase_04 AC 5 ms
11,620 KB
testcase_05 AC 5 ms
11,620 KB
testcase_06 AC 5 ms
11,620 KB
testcase_07 AC 4 ms
11,620 KB
testcase_08 AC 4 ms
11,620 KB
testcase_09 AC 5 ms
11,620 KB
testcase_10 AC 4 ms
11,624 KB
testcase_11 AC 5 ms
11,628 KB
testcase_12 AC 5 ms
11,632 KB
testcase_13 AC 5 ms
11,636 KB
testcase_14 AC 5 ms
11,636 KB
testcase_15 AC 9 ms
11,700 KB
testcase_16 AC 22 ms
11,776 KB
testcase_17 AC 44 ms
11,872 KB
testcase_18 AC 81 ms
11,968 KB
testcase_19 AC 99 ms
12,012 KB
testcase_20 AC 154 ms
12,096 KB
testcase_21 AC 148 ms
12,096 KB
testcase_22 AC 151 ms
12,100 KB
testcase_23 AC 191 ms
12,360 KB
testcase_24 AC 177 ms
12,224 KB
testcase_25 AC 173 ms
12,220 KB
testcase_26 AC 234 ms
12,712 KB
testcase_27 AC 232 ms
12,712 KB
testcase_28 AC 235 ms
12,712 KB
testcase_29 AC 230 ms
12,712 KB
testcase_30 AC 232 ms
12,712 KB
testcase_31 AC 228 ms
11,988 KB
testcase_32 AC 229 ms
11,988 KB
testcase_33 AC 228 ms
11,988 KB
testcase_34 AC 231 ms
11,988 KB
testcase_35 AC 229 ms
11,988 KB
testcase_36 AC 214 ms
11,992 KB
testcase_37 AC 111 ms
12,004 KB
testcase_38 AC 216 ms
11,992 KB
testcase_39 AC 189 ms
12,000 KB
testcase_40 AC 224 ms
11,988 KB
testcase_41 AC 5 ms
11,620 KB
testcase_42 AC 4 ms
11,620 KB
testcase_43 AC 5 ms
11,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_MOD_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}

vector<vector<int>>edges;
vector<vector<int>>childs;
vector<int>childnums;
int dfs(const int now,const int from) {
	int num = 1;
	for (const auto& e : edges[now]) {
		if (e != from) {
			childs[now].push_back(e);
			num += dfs(e,now);
		}
	}
	return childnums[now]=num;
}

map<int,Mod> getans(const int now) {
	map<int, Mod>nowmap;
	nowmap[0] = 1;
	for (const auto& e : childs[now]) {
		map<int, Mod>nextmap;
		nextmap = getans(e);
		map<int, Mod>newmap;
		for (auto i : nowmap) {
			for (auto j : nextmap) {
				newmap[i.first + j.first] += i.second*j.second;
			}
		}
		nowmap = newmap;
	}
	nowmap[childnums[now]] = 1;
	return nowmap;
}

int main() {
	int N, K; cin >> N >> K;
	edges.resize(N);
	childs.resize(N);
	childnums.resize(N);
	for (int i = 0; i < N - 1; ++i) {
		int a, b; cin >> a >> b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	dfs(0,-1);
	map<int, Mod>ansmap= getans(0);
	cout << ansmap[K] << endl;
	return 0;
}
0