1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #define maxn 100010 #define maxm 110 using namespace std;
const int p = 1000000007;
inline int add(int x, int y) { return (x += y) >= p ? x - p : x; }
int n, k;
struct Edge { int to, next; } e[maxn * 2]; int c1, head[maxn]; inline void add_edge(int u, int v) { e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
int f[maxn][maxm][2][2], g[maxm][2][2], sz[maxn]; void dfs(int u, int fa) { sz[u] = 1; f[u][0][0][0] = f[u][1][1][0] = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; dfs(v, u); for (int s = 0; s <= min(k, sz[u] + sz[v]); ++s) for (int o1 = 0; o1 < 2; ++o1) for (int o2 = 0; o2 < 2; ++o2) g[s][o1][o2] = 0; for (int s = 0; s <= min(k, sz[u]); ++s) for (int t = 0; t <= min(k, sz[v]) && s + t <= k; ++t) for (int p1 = 0; p1 < 2; ++p1) for (int p2 = 0; p2 < 2; ++p2) for (int q1 = 0; q1 < 2; ++q1) for (int q2 = 0; q2 < 2; ++q2) if (p1 | q2) g[s + t][p1][p2 | q1] = add(g[s + t][p1][p2 | q1], 1ll * f[u][s][p1][p2] * f[v][t][q1][q2] % p); sz[u] += sz[v]; for (int s = 0; s <= min(k, sz[u]); ++s) for (int o1 = 0; o1 < 2; ++o1) for (int o2 = 0; o2 < 2; ++o2) f[u][s][o1][o2] = g[s][o1][o2]; } }
int main() { fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> k; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; add_edge(x, y); add_edge(y, x); } dfs(1, 0); cout << add(f[1][k][0][1], f[1][k][1][1]) << "\n"; return 0; }
|