#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int mod = 1e9+7;
struct node{
int c=0;
array<int,2> a[2];
node(){a[0]=a[1]={0,0};}
friend node operator+(node x,node y){
node res;
res.c=x.c|y.c;
for(int i=0;i<=1;i++){
int ni=i|y.c;
(res.a[ni][0]+=x.a[i][0])%=mod;
(res.a[ni][1]+=x.a[i][1])%=mod;
ni=x.c|i;
(res.a[ni][0]+=y.a[i][0])%=mod;
(res.a[ni][1]+=y.a[i][1])%=mod;
}
return res;
}
void add(){
if(c) (a[1][0]+=1)%=mod,(a[1][1]+=1)%=mod;
else (a[1][0]+=1)%=mod,(a[0][1]+=1)%=mod;
}
}dp[maxn],sum[maxn];
int n;
ll D;
vector<int> edge[maxn];
node rev(node cur){
node res=cur;
res.c^=1;
swap(res.a[0],res.a[1]);
return res;
}
void dfs(int u,int p){
for(int v:edge[u]){
if(v==p) continue;
dfs(v,u);
sum[u]=sum[u]+rev(dp[v]);
}
dp[u]=sum[u];dp[u].add();
}
node val[maxn];
void redfs(int u,int p){
sum[u]=sum[u]+rev(val[u]);sum[u].add();
node cur;cur=cur+rev(val[u]);
for(int v:edge[u]){
if(v==p) continue;
val[v]=cur;cur=cur+rev(dp[v]);
}
cur=node();
reverse(edge[u].begin(),edge[u].end());
for(int v:edge[u]){
if(v==p) continue;
val[v]=val[v]+cur;
cur=cur+rev(dp[v]);
val[v].add();
redfs(v,u);
}
}
array<int,2> mul0(array<int,2> val,array<array<int,2>,2> ss){
array<int,2> res={0,0};
for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) (res[j]+=1LL*val[i]*ss[i][j]%mod)%=mod;
return res;
}
array<array<int,2>,2> mul1(array<array<int,2>,2> val,array<array<int,2>,2> ss){
array<array<int,2>,2> res={0,0};
for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) for(int k=0;k<=1;k++) (res[i][j]+=1LL*val[i][k]*ss[k][j]%mod)%=mod;
return res;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> D;
for(int i=1;i<n;i++){
int u,v;cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1,0);
val[1].c=1;
redfs(1,0);
array<array<int,2>,2> ss;
ss[0]=ss[1]={0,0};
for(int u=1;u<=n;u++){
for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) (ss[j][i]+=sum[u].a[i][j])%=mod;
}
array<int,2> val={0,0};
for(int u=1;u<=n;u++) val[sum[u].c]++;
D--;
while(D){
if(D&1) val=mul0(val,ss);
ss=mul1(ss,ss);D>>=1;
}
cout << (1LL*sum[1].a[1][0]*val[0]+1LL*sum[1].a[1][1]*val[1])%mod << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
3 ms |
8668 KB |
Output is correct |
3 |
Correct |
4 ms |
8540 KB |
Output is correct |
4 |
Correct |
4 ms |
8540 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8536 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8676 KB |
Output is correct |
5 |
Correct |
3 ms |
8676 KB |
Output is correct |
6 |
Correct |
3 ms |
8464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8536 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8676 KB |
Output is correct |
5 |
Correct |
3 ms |
8676 KB |
Output is correct |
6 |
Correct |
3 ms |
8464 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8536 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8676 KB |
Output is correct |
5 |
Correct |
3 ms |
8676 KB |
Output is correct |
6 |
Correct |
3 ms |
8464 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
60 ms |
22304 KB |
Output is correct |
13 |
Correct |
72 ms |
33228 KB |
Output is correct |
14 |
Correct |
52 ms |
12760 KB |
Output is correct |
15 |
Correct |
54 ms |
12884 KB |
Output is correct |
16 |
Correct |
57 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8536 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8676 KB |
Output is correct |
5 |
Correct |
3 ms |
8676 KB |
Output is correct |
6 |
Correct |
3 ms |
8464 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
8792 KB |
Output is correct |
13 |
Correct |
4 ms |
8540 KB |
Output is correct |
14 |
Correct |
4 ms |
8540 KB |
Output is correct |
15 |
Correct |
4 ms |
8640 KB |
Output is correct |
16 |
Correct |
4 ms |
8536 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8580 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
4 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8680 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
6 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8536 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8676 KB |
Output is correct |
5 |
Correct |
3 ms |
8676 KB |
Output is correct |
6 |
Correct |
3 ms |
8464 KB |
Output is correct |
7 |
Correct |
4 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
60 ms |
22304 KB |
Output is correct |
13 |
Correct |
72 ms |
33228 KB |
Output is correct |
14 |
Correct |
52 ms |
12760 KB |
Output is correct |
15 |
Correct |
54 ms |
12884 KB |
Output is correct |
16 |
Correct |
57 ms |
12888 KB |
Output is correct |
17 |
Correct |
4 ms |
8792 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8640 KB |
Output is correct |
21 |
Correct |
4 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
4 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8580 KB |
Output is correct |
27 |
Correct |
4 ms |
8796 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8680 KB |
Output is correct |
33 |
Correct |
4 ms |
8540 KB |
Output is correct |
34 |
Correct |
4 ms |
8540 KB |
Output is correct |
35 |
Correct |
6 ms |
8796 KB |
Output is correct |
36 |
Correct |
67 ms |
22100 KB |
Output is correct |
37 |
Correct |
85 ms |
33108 KB |
Output is correct |
38 |
Correct |
50 ms |
12756 KB |
Output is correct |
39 |
Correct |
59 ms |
12892 KB |
Output is correct |
40 |
Correct |
55 ms |
12828 KB |
Output is correct |
41 |
Correct |
65 ms |
27988 KB |
Output is correct |
42 |
Correct |
64 ms |
31060 KB |
Output is correct |
43 |
Correct |
47 ms |
12248 KB |
Output is correct |
44 |
Correct |
54 ms |
12784 KB |
Output is correct |
45 |
Correct |
57 ms |
12884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8668 KB |
Output is correct |
5 |
Correct |
4 ms |
8540 KB |
Output is correct |
6 |
Correct |
4 ms |
8540 KB |
Output is correct |
7 |
Correct |
4 ms |
8540 KB |
Output is correct |
8 |
Correct |
3 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8536 KB |
Output is correct |
10 |
Correct |
3 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8676 KB |
Output is correct |
12 |
Correct |
3 ms |
8676 KB |
Output is correct |
13 |
Correct |
3 ms |
8464 KB |
Output is correct |
14 |
Correct |
4 ms |
8796 KB |
Output is correct |
15 |
Correct |
4 ms |
8796 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
60 ms |
22304 KB |
Output is correct |
20 |
Correct |
72 ms |
33228 KB |
Output is correct |
21 |
Correct |
52 ms |
12760 KB |
Output is correct |
22 |
Correct |
54 ms |
12884 KB |
Output is correct |
23 |
Correct |
57 ms |
12888 KB |
Output is correct |
24 |
Correct |
4 ms |
8792 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8640 KB |
Output is correct |
28 |
Correct |
4 ms |
8536 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
4 ms |
8580 KB |
Output is correct |
34 |
Correct |
4 ms |
8796 KB |
Output is correct |
35 |
Correct |
4 ms |
8540 KB |
Output is correct |
36 |
Correct |
4 ms |
8540 KB |
Output is correct |
37 |
Correct |
4 ms |
8540 KB |
Output is correct |
38 |
Correct |
4 ms |
8540 KB |
Output is correct |
39 |
Correct |
4 ms |
8680 KB |
Output is correct |
40 |
Correct |
4 ms |
8540 KB |
Output is correct |
41 |
Correct |
4 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8796 KB |
Output is correct |
43 |
Correct |
67 ms |
22100 KB |
Output is correct |
44 |
Correct |
85 ms |
33108 KB |
Output is correct |
45 |
Correct |
50 ms |
12756 KB |
Output is correct |
46 |
Correct |
59 ms |
12892 KB |
Output is correct |
47 |
Correct |
55 ms |
12828 KB |
Output is correct |
48 |
Correct |
65 ms |
27988 KB |
Output is correct |
49 |
Correct |
64 ms |
31060 KB |
Output is correct |
50 |
Correct |
47 ms |
12248 KB |
Output is correct |
51 |
Correct |
54 ms |
12784 KB |
Output is correct |
52 |
Correct |
57 ms |
12884 KB |
Output is correct |
53 |
Correct |
79 ms |
33108 KB |
Output is correct |
54 |
Correct |
71 ms |
29124 KB |
Output is correct |
55 |
Correct |
46 ms |
11992 KB |
Output is correct |
56 |
Correct |
59 ms |
22100 KB |
Output is correct |
57 |
Correct |
57 ms |
12888 KB |
Output is correct |
58 |
Correct |
54 ms |
12880 KB |
Output is correct |
59 |
Correct |
57 ms |
12880 KB |
Output is correct |
60 |
Correct |
57 ms |
12892 KB |
Output is correct |