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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e4 + 10, mod = 1e9 + 7, K = 21;
int head[N], rest[N * 2], to[N * 2], tot; void add(int u, int v) { to[++ tot] = v, rest[tot] = head[u], head[u] = tot; } int n, k; ll ans[N], C[K][K], pw[N][K];
struct POI { ll a[K]; POI() { memset(a, 0, sizeof a); } void init(int x) { fill(a, a + k + 1, x); } ll &operator [] (int x) { return a[x]; } } poi_son[N], poi_fa[N]; POI operator + (POI a, POI b) { for(int i = 0 ; i <= k ; ++ i) a[i] = (a[i] + b[i]) % mod; return a; } POI operator - (POI a, POI b) { for(int i = 0 ; i <= k ; ++ i) a[i] = (a[i] - b[i]) % mod; return a; } POI operator + (POI a, ll x) { for(int i = k ; i >= 0 ; -- i) { ll sum = 0; for(int j = 0 ; j <= i ; ++ j) { sum = (sum + a[j] * pw[x][i - j] % mod * C[i][j]) % mod; } a[i] = sum; } return a; }
void dfs(int u, int fa) { poi_son[u].init(1); for(int i = head[u] ; i ; i = rest[i]) { int v = to[i]; if(v == fa) continue; dfs(v, u); poi_son[u] = poi_son[u] + (poi_son[v] + 1); } }
void sfd(int u, int fa) { if(fa) { poi_fa[u] = (poi_fa[fa] + 1) + (poi_son[fa] - (poi_son[u] + 1) + 1); } for(int i = head[u] ; i ; i = rest[i]) { int v = to[i]; if(v == fa) continue; sfd(v, u); } }
void sol() { dfs(1, 0); sfd(1, 0); for(int i = 1 ; i <= n ; ++ i) { ans[i] = (poi_son[i] + poi_fa[i])[k]; } }
int main() { C[0][0] = 1; for(int i = 1 ; i <= 20 ; ++ i) { C[i][0] = 1; for(int j = 1 ; j <= 20 ; ++ j) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } } for(int i = 0 ; i <= 20000 ; ++ i) { pw[i][0] = 1; for(int j = 1 ; j <= 20 ; ++ j) { pw[i][j] = pw[i][j - 1] * i % mod; } } while(scanf("%d%d", &n, &k) == 2) { for(int i = 1 ; i <= n ; ++ i) head[i] = ans[i] = 0; tot = 0; for(int i = 1, u, v ; i < n ; ++ i) { scanf("%d%d", &u, &v); add(u, v), add(v, u); } sol(); for(int i = 1 ; i <= n ; ++ i) printf("%lld\n", (ans[i] % mod + mod) % mod); puts(""); } }
|