#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> edges[N];
int dp[N], up[N], cnt[N], n;
int sub[N], rev[N], tot[N], tmp[N];
void dfs(int u, int p = -1){
for(auto x : edges[u]){
if(x == p) continue;
dfs(x, u);
if(dp[x] == 0){
dp[u] = 1,
tmp[u] += sub[x],
cnt[u]++;
}
tot[u] += sub[x];
}
if(cnt[u] > 1) sub[u] = 0;
if(cnt[u] == 1) sub[u] = tmp[u];
if(cnt[u] == 0) sub[u] = tot[u] + 1;
}
void re(int u, int p = -1){
tot[u] += rev[u];
if(up[u] == 0){
tmp[u] += rev[u];
dp[u] = 1, cnt[u]++;
}
if(cnt[u] > 1) sub[u] = 0;
if(cnt[u] == 1) sub[u] = tmp[u];
if(cnt[u] == 0) sub[u] = tot[u] + 1;
for(auto x : edges[u]){
if(x == p) continue;
tot[u] -= sub[x];
if(!dp[x]){
cnt[u]--, tmp[u] -= sub[x];
}
if(cnt[u]) up[x] = 1;
if(cnt[u] > 1) rev[x] = 0;
if(cnt[u] == 1) rev[x] = tmp[u];
if(cnt[u] == 0) rev[x] = tot[u] + 1;
tot[u] += sub[x];
if(!dp[x]){
cnt[u]++, tmp[u] += sub[x];
}
re(x, u);
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int d; cin>>n>>d;
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(1);
up[1] = 1;
re(1);
int E = 0, L = 0;
for(int i=1;i<=n;i++){
if(dp[i] == 1) E = (E + sub[i]) % mod;
else E = (E - sub[i] + mod) % mod, L++;
}
//~ for(int i=1;i<=n;i++){
//~ cout<<sub[i]<<" ";
//~ } cout<<"\n";
vector<int> e(d + 1), f(d + 1);
e[0] = f[0] = 1;
for(int i=1;i<=d;i++){
e[i] = e[i-1] * 1ll * E % mod;
f[i] = f[i-1] * 1ll * n % mod * n % mod;
}
d--;
int res = 0;
for(int i=0;i<=d;i++){
res = (res + f[d-i] * 1ll * e[i]) % mod;
}
res = res * 1ll * L % mod;
if(dp[1]){
res = res * 1ll * sub[1] % mod;
} else {
res = (f[d + 1] - res * 1ll * sub[1] % mod + mod) % mod;
}
res = (f[d + 1] - res + mod) % mod;
cout<<res<<"\n";
}
/*
5 1
1 2
2 3
2 4
1 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Runtime error |
5 ms |
5208 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
72 ms |
12048 KB |
Output is correct |
13 |
Correct |
65 ms |
16348 KB |
Output is correct |
14 |
Correct |
52 ms |
8668 KB |
Output is correct |
15 |
Correct |
70 ms |
8580 KB |
Output is correct |
16 |
Correct |
56 ms |
8548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
4 ms |
3388 KB |
Output is correct |
27 |
Correct |
4 ms |
3540 KB |
Output is correct |
28 |
Correct |
4 ms |
3540 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
3 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
72 ms |
12048 KB |
Output is correct |
13 |
Correct |
65 ms |
16348 KB |
Output is correct |
14 |
Correct |
52 ms |
8668 KB |
Output is correct |
15 |
Correct |
70 ms |
8580 KB |
Output is correct |
16 |
Correct |
56 ms |
8548 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
3388 KB |
Output is correct |
32 |
Correct |
4 ms |
3540 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3412 KB |
Output is correct |
35 |
Correct |
3 ms |
3156 KB |
Output is correct |
36 |
Correct |
67 ms |
13260 KB |
Output is correct |
37 |
Correct |
62 ms |
17464 KB |
Output is correct |
38 |
Correct |
47 ms |
9656 KB |
Output is correct |
39 |
Correct |
59 ms |
9844 KB |
Output is correct |
40 |
Correct |
58 ms |
9776 KB |
Output is correct |
41 |
Correct |
65 ms |
16120 KB |
Output is correct |
42 |
Correct |
60 ms |
16860 KB |
Output is correct |
43 |
Correct |
38 ms |
9500 KB |
Output is correct |
44 |
Correct |
53 ms |
9812 KB |
Output is correct |
45 |
Correct |
71 ms |
10368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Runtime error |
5 ms |
5208 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |