#include <bits/stdc++.h>
#define N 100005
using namespace std;
const int mod = 1e9 + 7;
long long binpow(long long a,long long b){
long long ret = 1;
while(b){
if(b & 1)
ret = ret * a %mod;
a = a * a %mod;
b >>=1;
}
return ret;
}
struct node{
long long a,b,sz,sz2;
node(){
a = b = sz = sz2 = 0;
}
};
struct matrix{
long long a[3][3];
matrix(){
for(int i = 0;i<3;i++){
for(int j = 0;j<3;j++){
a[i][j] = 0;
}
}
}
matrix operator*(matrix other){
matrix ret;
for(int i = 0;i<3;i++){
for(int j =0;j<3;j++){
for(int k = 0;k<3;k++){
ret.a[i][k] = (ret.a[i][k] + a[i][j]*other.a[j][k])%mod;
}
}
}
return ret;
}
};
matrix binexpo(matrix a,long long b){
matrix ret;
for(int i = 0;i<3;i++)
ret.a[i][i] = 1;
while(b){
if(b&1)
ret = ret * a;
a = a * a;
b>>=1;
}
return ret;
}
vector<int> adj[N];
long long ans = 0;
long long n,d;
int win[N];
int sub[N];
int top[N];
int len[N];
int sum = 0;
int timer = 1;
int tin[N],tout[N];
bool dfs(int v,int par){
tin[v] = timer++;
sub[v] = 0;
for(auto u:adj[v]){
if(u == par)continue;
if(dfs(u,v) == 0){
win[v] = 1;
sub[v] = 1;
}
}
tout[v] = timer - 1;
len[v] = tout[v] - tin[v] + 1;
return sub[v];
}
void dfs2(int v,int par){
int cnt = !top[v];
for(auto u:adj[v]){
if(u == par)continue;
cnt += !sub[u];
}
for(auto u:adj[v]){
if(u == par)continue;
cnt -= !sub[u];
if(!cnt){
win[u] = 1;
top[u] = 0;
}
cnt += !sub[u];
dfs2(u,v);
}
}
node subval[N];
node val[N];
node topval[N];
void merge(node &a,node b){
a.a += b.b;
a.b += b.a;
a.sz += b.sz2;
a.sz2 += b.sz;
a.sz %= mod;
a.sz2 %= mod;
}
void antimerge(node &a,node b){
a.a -= b.b;
a.b -= b.a;
a.sz -= b.sz2;
a.sz2 -= b.sz;
a.sz = (a.sz + mod)%mod;
a.sz2 = (a.sz2 + mod)%mod;
}
void dfs3(int v,int par){
vector<int> places;
subval[v] = node();
for(auto u:adj[v]){
if(u == par)continue;
if(!sub[u]){
places.push_back(u);
}
dfs3(u,v);
}
if(places.empty()){
subval[v].b++;
for(auto u:adj[v]){
if(u == par)continue;
merge(subval[v],subval[u]);
}
}
if(places.size() == 1){
subval[v].sz += n * (len[v] - len[places[0]]);
subval[v].sz %= mod;
merge(subval[v],subval[places[0]]);
}
if(places.size() > 1){
subval[v].sz += n * len[v];
subval[v].sz %= mod;
}
}
void dfs4(int v,int par){
vector<int> places;
if(!top[v])
places.push_back(par);
val[v] = node();
for(auto u:adj[v]){
if(u == par)continue;
if(!sub[u]){
places.push_back(u);
}
}
if(places.empty()){
val[v].b++;
merge(val[v],topval[v]);
for(auto u:adj[v]){
if(u == par)continue;
merge(val[v],subval[u]);
}
}
if(places.size() == 1){
if(places[0] == par){
val[v].sz += n * len[v];
val[v].sz %= mod;
merge(val[v],topval[v]);
}
else{
val[v].sz += n * (n - len[places[0]]);
val[v].sz %= mod;
merge(val[v],subval[places[0]]);
}
}
if(places.size() > 1){
val[v].sz += n * n;
val[v].sz %= mod;
}
node sum;
merge(sum,topval[v]);
for(auto u:adj[v]){
if(u == par)continue;
merge(sum,subval[u]);
}
for(auto u:adj[v]){
if(u == par)continue;
antimerge(sum,subval[u]);
node tmp = sum;
int x = places.size() - !sub[u];
if(x == 0){
tmp.b++;
}
if(x == 1){
tmp = node();
int pos = places[0];
if(pos == u)
pos = places[1];
if(pos == par){
tmp.sz += n * ( len[v] - len[u]);
tmp.sz %= mod;
merge(tmp,topval[v]);
}
else{
tmp.sz += n * (n - len[pos] - len[u]);
tmp.sz %= mod;
merge(tmp,subval[pos]);
}
}
if(x > 1){
tmp = node();
tmp.sz += n * (n - len[u]);
tmp.sz %= mod;
}
topval[u] = tmp;
merge(sum,subval[u]);
dfs4(u,v);
}
}
void solve(){
cin >> n >> d;
for(int i = 1;i<n;i++){
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<node> v;
long long winstate = 0;
for(int i = 1;i<=n;i++){
win[i] = 0;
sub[i] = 0;
top[i] = 1;
}
dfs(1,0);
dfs2(1,0);
dfs3(1,0);
dfs4(1,0);
for(int x = 1;x<=n;x++){
v.push_back(val[x]);
}
long long suma = 0,sumb = 0,sumsz = 0;
for(auto u:v){
suma += u.a;
sumb += u.b;
sumsz += u.sz;
suma %= mod;
sumb %= mod;
sumsz %= mod;
}
for(int i = 1;i<=n;i++){
winstate += win[i];
}
matrix single;
single.a[0][0] = (suma - sumb +mod)%mod;
single.a[1][0] = (sumb)%mod;
single.a[2][0] = (sumsz)%mod;
single.a[1][1] = n*n%mod;
single.a[2][2] = n*n%mod;
matrix total = binexpo(single,d-1);
long long val = 0;
val = (val + total.a[0][0] * winstate)%mod;
val = (val + total.a[1][0] * n)%mod;
val = (val + total.a[2][0] * 1)%mod;
winstate = val;
/*
for(int i = 0;i<d-1;i++){
winstate = (winstate * (suma - sumb + mod) + binpow(n,2*i+1)*sumb + binpow(n,2*i)*sumsz)%mod;
}*/
winstate = (winstate * (v[0].a - v[0].b + mod) + binpow(n,2*d-1)*v[0].b + binpow(n,2*d-2)*v[0].sz)%mod;
cout << winstate;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0 * clock() / CLOCKS_PER_SEC << " milliseconds.";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12076 KB |
Output is correct |
2 |
Correct |
6 ms |
12184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12072 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
12076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12244 KB |
Output is correct |
8 |
Correct |
6 ms |
12372 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12148 KB |
Output is correct |
11 |
Correct |
6 ms |
12200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12244 KB |
Output is correct |
8 |
Correct |
6 ms |
12372 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12148 KB |
Output is correct |
11 |
Correct |
6 ms |
12200 KB |
Output is correct |
12 |
Correct |
124 ms |
31380 KB |
Output is correct |
13 |
Correct |
131 ms |
41624 KB |
Output is correct |
14 |
Correct |
120 ms |
22912 KB |
Output is correct |
15 |
Correct |
99 ms |
22952 KB |
Output is correct |
16 |
Correct |
102 ms |
22956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12244 KB |
Output is correct |
8 |
Correct |
6 ms |
12372 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12148 KB |
Output is correct |
11 |
Correct |
6 ms |
12200 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
12204 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
6 ms |
12076 KB |
Output is correct |
16 |
Correct |
6 ms |
12076 KB |
Output is correct |
17 |
Correct |
6 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
12080 KB |
Output is correct |
21 |
Correct |
7 ms |
12204 KB |
Output is correct |
22 |
Correct |
6 ms |
12372 KB |
Output is correct |
23 |
Correct |
6 ms |
12200 KB |
Output is correct |
24 |
Correct |
6 ms |
12204 KB |
Output is correct |
25 |
Correct |
7 ms |
12204 KB |
Output is correct |
26 |
Correct |
6 ms |
12244 KB |
Output is correct |
27 |
Correct |
9 ms |
12344 KB |
Output is correct |
28 |
Correct |
9 ms |
12196 KB |
Output is correct |
29 |
Correct |
6 ms |
12116 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12244 KB |
Output is correct |
8 |
Correct |
6 ms |
12372 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12148 KB |
Output is correct |
11 |
Correct |
6 ms |
12200 KB |
Output is correct |
12 |
Correct |
124 ms |
31380 KB |
Output is correct |
13 |
Correct |
131 ms |
41624 KB |
Output is correct |
14 |
Correct |
120 ms |
22912 KB |
Output is correct |
15 |
Correct |
99 ms |
22952 KB |
Output is correct |
16 |
Correct |
102 ms |
22956 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
12204 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
12076 KB |
Output is correct |
21 |
Correct |
6 ms |
12076 KB |
Output is correct |
22 |
Correct |
6 ms |
12116 KB |
Output is correct |
23 |
Correct |
6 ms |
11988 KB |
Output is correct |
24 |
Correct |
6 ms |
11988 KB |
Output is correct |
25 |
Correct |
7 ms |
12080 KB |
Output is correct |
26 |
Correct |
7 ms |
12204 KB |
Output is correct |
27 |
Correct |
6 ms |
12372 KB |
Output is correct |
28 |
Correct |
6 ms |
12200 KB |
Output is correct |
29 |
Correct |
6 ms |
12204 KB |
Output is correct |
30 |
Correct |
7 ms |
12204 KB |
Output is correct |
31 |
Correct |
6 ms |
12244 KB |
Output is correct |
32 |
Correct |
9 ms |
12344 KB |
Output is correct |
33 |
Correct |
9 ms |
12196 KB |
Output is correct |
34 |
Correct |
6 ms |
12116 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
131 ms |
31504 KB |
Output is correct |
37 |
Correct |
131 ms |
41484 KB |
Output is correct |
38 |
Correct |
82 ms |
22912 KB |
Output is correct |
39 |
Correct |
108 ms |
22996 KB |
Output is correct |
40 |
Correct |
102 ms |
22972 KB |
Output is correct |
41 |
Correct |
115 ms |
36640 KB |
Output is correct |
42 |
Correct |
120 ms |
39336 KB |
Output is correct |
43 |
Correct |
72 ms |
22092 KB |
Output is correct |
44 |
Correct |
110 ms |
22952 KB |
Output is correct |
45 |
Correct |
119 ms |
22972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12076 KB |
Output is correct |
2 |
Correct |
6 ms |
12184 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
12072 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12076 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12244 KB |
Output is correct |
15 |
Correct |
6 ms |
12372 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12148 KB |
Output is correct |
18 |
Correct |
6 ms |
12200 KB |
Output is correct |
19 |
Correct |
124 ms |
31380 KB |
Output is correct |
20 |
Correct |
131 ms |
41624 KB |
Output is correct |
21 |
Correct |
120 ms |
22912 KB |
Output is correct |
22 |
Correct |
99 ms |
22952 KB |
Output is correct |
23 |
Correct |
102 ms |
22956 KB |
Output is correct |
24 |
Correct |
6 ms |
11988 KB |
Output is correct |
25 |
Correct |
6 ms |
12204 KB |
Output is correct |
26 |
Correct |
6 ms |
11988 KB |
Output is correct |
27 |
Correct |
6 ms |
12076 KB |
Output is correct |
28 |
Correct |
6 ms |
12076 KB |
Output is correct |
29 |
Correct |
6 ms |
12116 KB |
Output is correct |
30 |
Correct |
6 ms |
11988 KB |
Output is correct |
31 |
Correct |
6 ms |
11988 KB |
Output is correct |
32 |
Correct |
7 ms |
12080 KB |
Output is correct |
33 |
Correct |
7 ms |
12204 KB |
Output is correct |
34 |
Correct |
6 ms |
12372 KB |
Output is correct |
35 |
Correct |
6 ms |
12200 KB |
Output is correct |
36 |
Correct |
6 ms |
12204 KB |
Output is correct |
37 |
Correct |
7 ms |
12204 KB |
Output is correct |
38 |
Correct |
6 ms |
12244 KB |
Output is correct |
39 |
Correct |
9 ms |
12344 KB |
Output is correct |
40 |
Correct |
9 ms |
12196 KB |
Output is correct |
41 |
Correct |
6 ms |
12116 KB |
Output is correct |
42 |
Correct |
7 ms |
12116 KB |
Output is correct |
43 |
Correct |
131 ms |
31504 KB |
Output is correct |
44 |
Correct |
131 ms |
41484 KB |
Output is correct |
45 |
Correct |
82 ms |
22912 KB |
Output is correct |
46 |
Correct |
108 ms |
22996 KB |
Output is correct |
47 |
Correct |
102 ms |
22972 KB |
Output is correct |
48 |
Correct |
115 ms |
36640 KB |
Output is correct |
49 |
Correct |
120 ms |
39336 KB |
Output is correct |
50 |
Correct |
72 ms |
22092 KB |
Output is correct |
51 |
Correct |
110 ms |
22952 KB |
Output is correct |
52 |
Correct |
119 ms |
22972 KB |
Output is correct |
53 |
Correct |
126 ms |
41496 KB |
Output is correct |
54 |
Correct |
119 ms |
37680 KB |
Output is correct |
55 |
Correct |
68 ms |
21624 KB |
Output is correct |
56 |
Correct |
121 ms |
30660 KB |
Output is correct |
57 |
Correct |
97 ms |
23000 KB |
Output is correct |
58 |
Correct |
96 ms |
22972 KB |
Output is correct |
59 |
Correct |
100 ms |
22976 KB |
Output is correct |
60 |
Correct |
94 ms |
22976 KB |
Output is correct |