結果

問題 No.196 典型DP (1)
ユーザー yumakmcyumakmc
提出日時 2016-02-21 21:34:47
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 3,488 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 179,292 KB
実行使用メモリ 12,548 KB
最終ジャッジ日時 2024-09-22 12:36:16
合計ジャッジ時間 7,686 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0