#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 5e5+10;
const int mod = 1e9+7;
int n, d, dpw[maxn][2], dpl[maxn][2], dpwup[maxn][2], dplup[maxn][2];
vector<int> g[maxn];
int SL, SW, SL0, SW0, SWb, SLb;
void dfs1(int u, int ant) {
dpw[u][1] = SW*dpw[u][0] + SL*(dpw[u][0]+dpl[u][0]); dpw[u][1]%= mod;
dpl[u][1] = SW*dpl[u][0]; dpl[u][1]%= mod;
int qtl = 0;
for(auto v : g[u]) if(v != ant) {
qtl+= dpl[v][0];
dfs1(v,u);
}
for(auto v : g[u]) if(v != ant) {
qtl-= dpl[v][0];
if(qtl) {
dpw[u][1]+= dpw[v][1]+dpl[v][1]; dpw[u][1]%= mod;
dpl[u][1]+= 0;
}
else {
dpw[u][1]+= dpl[v][1]; dpw[u][1]%= mod;
dpl[u][1]+= dpw[v][1]; dpl[u][1]%= mod;
}
qtl+= dpl[v][0];
}
}
int dw[maxn][2], dl[maxn][2], sdw[maxn][2], sdl[maxn][2], sbsz[maxn];
set<int> dl0s[maxn];
void calc(int u) {
if(sdl[u][0] >= 1) {
dw[u][0] = 1;
}
else {
dw[u][0] = 0;
}
if(sdl[u][0] >= 2) {
dw[u][1] = 1+sdw[u][1]+sdl[u][1];
}
else if(sdl[u][0] == 1) {
dw[u][1] = 1+sdw[u][1]+sdl[u][1];
int v1 = *dl0s[u].begin();
dw[u][1]-= dw[v1][1];
}
else {
dw[u][1] = 1+sdl[u][1];
}
dl[u][0] = 1-dw[u][0];
dl[u][1] = sbsz[u]-dw[u][1];
}
void sol(int u, int ant) {
dw[u][0] = 0;
dl[u][0] = 0;
sdw[u][0] = 0;
sdl[u][0] = 0;
dw[u][1] = 0;
dl[u][1] = 0;
sdw[u][1] = 0;
sdl[u][1] = 0;
sbsz[u] = 1;
dl0s[u].clear();
for(auto v : g[u]) if(v != ant) {
sol(v,u);
sdw[u][0]+= dw[v][0];
sdw[u][1]+= dw[v][1];
sdl[u][0]+= dl[v][0];
sdl[u][1]+= dl[v][1];
sbsz[u]+= sbsz[v];
if(dl[v][0]) {
dl0s[u].insert(v);
}
}
calc(u);
}
void dfsrrt(int u, int ant) {
SWb+= dw[u][1];
for(auto v : g[u]) if(v != ant) {
// take v off u
sdw[u][0]-= dw[v][0];
sdw[u][1]-= dw[v][1];
sdl[u][0]-= dl[v][0];
sdl[u][1]-= dl[v][1];
sbsz[u]-= sbsz[v];
if(dl[v][0]) {
dl0s[u].erase(v);
}
calc(u);
// put u into v
sdw[v][0]+= dw[u][0];
sdw[v][1]+= dw[u][1];
sdl[v][0]+= dl[u][0];
sdl[v][1]+= dl[u][1];
sbsz[v]+= sbsz[u];
if(dl[u][0]) {
dl0s[v].insert(u);
}
calc(v);
dfsrrt(v,u);
// take u off v
sdw[v][0]-= dw[u][0];
sdw[v][1]-= dw[u][1];
sdl[v][0]-= dl[u][0];
sdl[v][1]-= dl[u][1];
sbsz[v]-= sbsz[u];
if(dl[u][0]) {
dl0s[v].erase(u);
}
calc(v);
// put v into u
sdw[u][0]+= dw[v][0];
sdw[u][1]+= dw[v][1];
sdl[u][0]+= dl[v][0];
sdl[u][1]+= dl[v][1];
sbsz[u]+= sbsz[v];
if(dl[v][0]) {
dl0s[u].insert(v);
}
calc(u);
}
}
void dfs0(int u, int ant) {
dpw[u][0] = 0;
int qtl = 0;
for(auto v : g[u]) if(v != ant) {
dfs0(v,u);
qtl+= dpl[v][0];
}
if(qtl) dpw[u][0] = 1;
dpl[u][0] = 1-dpw[u][0];
}
void dfs0up(int u, int ant) {
int qtl = 0;
for(auto v : g[u]) if(v != ant) {
qtl+= dpl[v][0];
}
for(auto v : g[u]) if(v != ant) {
qtl-= dpl[v][0];
if(qtl || dpwup[u][0]) {
dpwup[v][0] = 0;
dplup[v][0] = 1-dpwup[v][0];
}
else {
dpwup[v][0] = 1;
dplup[v][0] = 1-dpwup[v][0];
}
qtl+= dpl[v][0];
}
for(auto v : g[u]) if(v != ant) {
dfs0up(v,u);
}
}
void solve() {
cin >> n >> d;
for(int i = 1; i <= n-1; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs0(1,0);
dpwup[1][0] = 0;
dplup[1][0] = 1;
dfs0up(1,0);
for(int i = 1; i <= n; i++) {
if(dpw[i][0] || dpwup[i][0]) SW0++;
else SL0++;
}
sol(1,0);
dfsrrt(1,0);
SLb = n*n-SWb;
// cout << SWb << " " << SLb << endl;
// cout << SW0 << " " << SL0 << endl;
SWb%= mod;
SLb%= mod;
SW = SW0;
SL = SL0;
// for(int i = 1; i <= d-1; i++) {
// int newSW = n*(SW*SW0%mod)%mod + SL*SWb; newSW%= mod;
// int newSL = n*(SW*SL0%mod)%mod + SL*SLb; newSL%= mod;
// SW = newSW;
// SL = newSL;
// }
int r[1][2];
r[0][0] = SW;
r[0][1] = SL;
int b[2][2];
b[0][0] = n*SW0%mod;
b[0][1] = n*SL0%mod;
b[1][0] = SWb;
b[1][1] = SLb;
for(int pt = 0; pt <= 60; pt++) {
if((1LL<<pt)&(d-1)) {
int a[1][2];
a[0][0] = r[0][0];
a[0][1] = r[0][1];
r[0][0] = 0;
r[0][1] = 0;
for(int k = 0; k <= 1; k++) {
for(int i = 0; i <= 0; i++) {
for(int j = 0; j <= 1; j++) {
r[i][j]+= a[i][k]*b[k][j];
r[i][j]%= mod;
}
}
}
}
int rb[2][2];
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
rb[i][j] = 0;
}
}
for(int k = 0; k <= 1; k++) {
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
rb[i][j]+= b[i][k]*b[k][j];
rb[i][j]%= mod;
}
}
}
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
b[i][j] = rb[i][j];
}
}
}
SW = r[0][0];
SL = r[0][1];
dfs1(1,0);
// cout << SW << " " << SL << endl;
cout << dpw[1][1] << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
16 ms |
35796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
35704 KB |
Output is correct |
2 |
Correct |
16 ms |
35504 KB |
Output is correct |
3 |
Correct |
17 ms |
35540 KB |
Output is correct |
4 |
Correct |
16 ms |
35512 KB |
Output is correct |
5 |
Correct |
17 ms |
35512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
16 ms |
35640 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
17 ms |
35608 KB |
Output is correct |
6 |
Correct |
17 ms |
35600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
16 ms |
35640 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
17 ms |
35608 KB |
Output is correct |
6 |
Correct |
17 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35796 KB |
Output is correct |
8 |
Correct |
19 ms |
35924 KB |
Output is correct |
9 |
Correct |
17 ms |
35796 KB |
Output is correct |
10 |
Correct |
18 ms |
35784 KB |
Output is correct |
11 |
Correct |
18 ms |
35752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
16 ms |
35640 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
17 ms |
35608 KB |
Output is correct |
6 |
Correct |
17 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35796 KB |
Output is correct |
8 |
Correct |
19 ms |
35924 KB |
Output is correct |
9 |
Correct |
17 ms |
35796 KB |
Output is correct |
10 |
Correct |
18 ms |
35784 KB |
Output is correct |
11 |
Correct |
18 ms |
35752 KB |
Output is correct |
12 |
Correct |
162 ms |
61060 KB |
Output is correct |
13 |
Correct |
167 ms |
68420 KB |
Output is correct |
14 |
Correct |
123 ms |
54776 KB |
Output is correct |
15 |
Correct |
142 ms |
54764 KB |
Output is correct |
16 |
Correct |
141 ms |
55100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
16 ms |
35640 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
17 ms |
35608 KB |
Output is correct |
6 |
Correct |
17 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35796 KB |
Output is correct |
8 |
Correct |
19 ms |
35924 KB |
Output is correct |
9 |
Correct |
17 ms |
35796 KB |
Output is correct |
10 |
Correct |
18 ms |
35784 KB |
Output is correct |
11 |
Correct |
18 ms |
35752 KB |
Output is correct |
12 |
Correct |
17 ms |
35588 KB |
Output is correct |
13 |
Correct |
18 ms |
35740 KB |
Output is correct |
14 |
Correct |
17 ms |
35492 KB |
Output is correct |
15 |
Correct |
17 ms |
35600 KB |
Output is correct |
16 |
Correct |
17 ms |
35572 KB |
Output is correct |
17 |
Correct |
20 ms |
35556 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35668 KB |
Output is correct |
21 |
Correct |
24 ms |
35752 KB |
Output is correct |
22 |
Correct |
17 ms |
35944 KB |
Output is correct |
23 |
Correct |
17 ms |
35684 KB |
Output is correct |
24 |
Correct |
17 ms |
35796 KB |
Output is correct |
25 |
Correct |
18 ms |
35796 KB |
Output is correct |
26 |
Correct |
18 ms |
35768 KB |
Output is correct |
27 |
Correct |
17 ms |
35924 KB |
Output is correct |
28 |
Correct |
21 ms |
35768 KB |
Output is correct |
29 |
Correct |
17 ms |
35708 KB |
Output is correct |
30 |
Correct |
17 ms |
35796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
16 ms |
35640 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
17 ms |
35608 KB |
Output is correct |
6 |
Correct |
17 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35796 KB |
Output is correct |
8 |
Correct |
19 ms |
35924 KB |
Output is correct |
9 |
Correct |
17 ms |
35796 KB |
Output is correct |
10 |
Correct |
18 ms |
35784 KB |
Output is correct |
11 |
Correct |
18 ms |
35752 KB |
Output is correct |
12 |
Correct |
162 ms |
61060 KB |
Output is correct |
13 |
Correct |
167 ms |
68420 KB |
Output is correct |
14 |
Correct |
123 ms |
54776 KB |
Output is correct |
15 |
Correct |
142 ms |
54764 KB |
Output is correct |
16 |
Correct |
141 ms |
55100 KB |
Output is correct |
17 |
Correct |
17 ms |
35588 KB |
Output is correct |
18 |
Correct |
18 ms |
35740 KB |
Output is correct |
19 |
Correct |
17 ms |
35492 KB |
Output is correct |
20 |
Correct |
17 ms |
35600 KB |
Output is correct |
21 |
Correct |
17 ms |
35572 KB |
Output is correct |
22 |
Correct |
20 ms |
35556 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
18 ms |
35540 KB |
Output is correct |
25 |
Correct |
18 ms |
35668 KB |
Output is correct |
26 |
Correct |
24 ms |
35752 KB |
Output is correct |
27 |
Correct |
17 ms |
35944 KB |
Output is correct |
28 |
Correct |
17 ms |
35684 KB |
Output is correct |
29 |
Correct |
17 ms |
35796 KB |
Output is correct |
30 |
Correct |
18 ms |
35796 KB |
Output is correct |
31 |
Correct |
18 ms |
35768 KB |
Output is correct |
32 |
Correct |
17 ms |
35924 KB |
Output is correct |
33 |
Correct |
21 ms |
35768 KB |
Output is correct |
34 |
Correct |
17 ms |
35708 KB |
Output is correct |
35 |
Correct |
17 ms |
35796 KB |
Output is correct |
36 |
Correct |
163 ms |
61048 KB |
Output is correct |
37 |
Correct |
172 ms |
68428 KB |
Output is correct |
38 |
Correct |
127 ms |
54832 KB |
Output is correct |
39 |
Correct |
139 ms |
54736 KB |
Output is correct |
40 |
Correct |
141 ms |
55116 KB |
Output is correct |
41 |
Correct |
159 ms |
65068 KB |
Output is correct |
42 |
Correct |
143 ms |
65472 KB |
Output is correct |
43 |
Correct |
102 ms |
52516 KB |
Output is correct |
44 |
Correct |
149 ms |
55084 KB |
Output is correct |
45 |
Correct |
138 ms |
54824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
16 ms |
35796 KB |
Output is correct |
3 |
Correct |
17 ms |
35704 KB |
Output is correct |
4 |
Correct |
16 ms |
35504 KB |
Output is correct |
5 |
Correct |
17 ms |
35540 KB |
Output is correct |
6 |
Correct |
16 ms |
35512 KB |
Output is correct |
7 |
Correct |
17 ms |
35512 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
16 ms |
35640 KB |
Output is correct |
11 |
Correct |
17 ms |
35540 KB |
Output is correct |
12 |
Correct |
17 ms |
35608 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
16 ms |
35796 KB |
Output is correct |
15 |
Correct |
19 ms |
35924 KB |
Output is correct |
16 |
Correct |
17 ms |
35796 KB |
Output is correct |
17 |
Correct |
18 ms |
35784 KB |
Output is correct |
18 |
Correct |
18 ms |
35752 KB |
Output is correct |
19 |
Correct |
162 ms |
61060 KB |
Output is correct |
20 |
Correct |
167 ms |
68420 KB |
Output is correct |
21 |
Correct |
123 ms |
54776 KB |
Output is correct |
22 |
Correct |
142 ms |
54764 KB |
Output is correct |
23 |
Correct |
141 ms |
55100 KB |
Output is correct |
24 |
Correct |
17 ms |
35588 KB |
Output is correct |
25 |
Correct |
18 ms |
35740 KB |
Output is correct |
26 |
Correct |
17 ms |
35492 KB |
Output is correct |
27 |
Correct |
17 ms |
35600 KB |
Output is correct |
28 |
Correct |
17 ms |
35572 KB |
Output is correct |
29 |
Correct |
20 ms |
35556 KB |
Output is correct |
30 |
Correct |
17 ms |
35540 KB |
Output is correct |
31 |
Correct |
18 ms |
35540 KB |
Output is correct |
32 |
Correct |
18 ms |
35668 KB |
Output is correct |
33 |
Correct |
24 ms |
35752 KB |
Output is correct |
34 |
Correct |
17 ms |
35944 KB |
Output is correct |
35 |
Correct |
17 ms |
35684 KB |
Output is correct |
36 |
Correct |
17 ms |
35796 KB |
Output is correct |
37 |
Correct |
18 ms |
35796 KB |
Output is correct |
38 |
Correct |
18 ms |
35768 KB |
Output is correct |
39 |
Correct |
17 ms |
35924 KB |
Output is correct |
40 |
Correct |
21 ms |
35768 KB |
Output is correct |
41 |
Correct |
17 ms |
35708 KB |
Output is correct |
42 |
Correct |
17 ms |
35796 KB |
Output is correct |
43 |
Correct |
163 ms |
61048 KB |
Output is correct |
44 |
Correct |
172 ms |
68428 KB |
Output is correct |
45 |
Correct |
127 ms |
54832 KB |
Output is correct |
46 |
Correct |
139 ms |
54736 KB |
Output is correct |
47 |
Correct |
141 ms |
55116 KB |
Output is correct |
48 |
Correct |
159 ms |
65068 KB |
Output is correct |
49 |
Correct |
143 ms |
65472 KB |
Output is correct |
50 |
Correct |
102 ms |
52516 KB |
Output is correct |
51 |
Correct |
149 ms |
55084 KB |
Output is correct |
52 |
Correct |
138 ms |
54824 KB |
Output is correct |
53 |
Correct |
177 ms |
69576 KB |
Output is correct |
54 |
Correct |
166 ms |
66848 KB |
Output is correct |
55 |
Correct |
95 ms |
51760 KB |
Output is correct |
56 |
Correct |
154 ms |
62464 KB |
Output is correct |
57 |
Correct |
140 ms |
56652 KB |
Output is correct |
58 |
Correct |
139 ms |
56556 KB |
Output is correct |
59 |
Correct |
145 ms |
55848 KB |
Output is correct |
60 |
Correct |
168 ms |
55824 KB |
Output is correct |