#include <iostream>
#include <vector>
using namespace std;
/*
Note that this is an O(N+logD) solution
Made for you by Mate Busa
*/
const long long mod = 1000000007;
long long N, D, a, b, L, W, LC, WC, sL, sW, sLC, sWC;
vector<int> neighbour[100000];
bool lose[100000];
bool visited[100000];
long long c[100000];
long long oW[100000];
long long oL[100000];
long long h[100000];
//calculate the state for all roots
void dfs(int x){
visited[x] = true; lose[x] = true;
for(int i:neighbour[x]){
if(!visited[i]){
dfs(i);
if(lose[i]) h[x]++;
}
}
if(h[x]!=0) lose[x] = false;
}
void critical(int x){
if(lose[x]){
c[x] = oW[x] + 1;
} else {
if(h[x]==1){
c[x] = oL[x];
} else {
c[x] = 0;
}
}
}
//calculate critical states
void dfs2(int x){
visited[x] = true;
for(int i:neighbour[x]){
if(!visited[i]){
dfs2(i);
if(lose[i]){
oL[x] += c[i];
} else {
oW[x] += c[i];
}
}
}
critical(x);
}
//reroot the tree in a neighbour
void reroot(int x, int y){
if(lose[y]){
h[x]--; oL[x] -= c[y];
} else {
oW[x] -= c[y];
}
if(h[x]==0) lose[x] = true;
critical(x);
if(lose[x]){
h[y]++; oL[y] += c[x];
} else {
oW[y] += c[x];
}
if(h[y]!=0) lose[y] = false;
critical(y);
}
void evaluate(int x){
visited[x] = true;
if(lose[x]){
L++; LC = (LC+c[x])%mod;
} else {
W++; WC = (WC+c[x])%mod;
}
for(int i:neighbour[x]){
if(!visited[i]){
reroot(x,i);
evaluate(i);
reroot(i,x);
}
}
}
//a bonus dimension
void step(long long pl, long long plc, long long pw, long long pwc){
sL = ((((pl * N) % mod) * W) % mod + (pwc * L) % mod + ((((pl * N)%mod - plc + mod)%mod) * L)%mod) % mod;
sLC = (plc * WC + pwc * LC) % mod;
sW = ((((pw * N) % mod) * W) % mod + (plc * L) % mod + ((((pw * N)%mod - pwc + mod)%mod) * L)%mod) % mod;
sWC = (pwc * WC + plc * LC) % mod;
}
//double up the number of dimensions
void step2(long long pl, long long plc, long long pw, long long pwc){
L = ((((pl * N) % mod) * pw) % mod + (pwc * pl) % mod + ((((pl * N)%mod - plc + mod)%mod) * pl)%mod) % mod;
LC = (plc * pwc + pwc * plc) % mod;
W = ((((pw * N) % mod) * pw) % mod + (plc * pl) % mod + ((((pw * N)%mod - pwc + mod)%mod) * pl)%mod) % mod;
WC = (pwc * pwc + plc * plc) % mod;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N >> D;
for(int i=0; i<N-1; i++){
cin >> a >> b;
neighbour[a-1].push_back(b-1);
neighbour[b-1].push_back(a-1);
}
dfs(0);
for(int i=0; i<N; i++) visited[i] = false;
dfs2(0);
for(int i=0; i<N; i++) visited[i] = false;
evaluate(0);
if(lose[0]){
sL = 1; sLC = c[0];
} else {
sW = 1; sWC = c[0];
}
//use binary representation of D
while(D>0){
if(D%2==1) step(sL, sLC, sW, sWC);
step2(L,LC,W,WC); D /= 2;
}
cout << sW << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
94 ms |
11352 KB |
Output is correct |
13 |
Correct |
100 ms |
13880 KB |
Output is correct |
14 |
Correct |
78 ms |
9332 KB |
Output is correct |
15 |
Correct |
111 ms |
9208 KB |
Output is correct |
16 |
Correct |
106 ms |
9208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2816 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
2 ms |
2816 KB |
Output is correct |
24 |
Correct |
2 ms |
2816 KB |
Output is correct |
25 |
Correct |
2 ms |
2816 KB |
Output is correct |
26 |
Correct |
2 ms |
2816 KB |
Output is correct |
27 |
Correct |
2 ms |
2816 KB |
Output is correct |
28 |
Correct |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
2 ms |
2816 KB |
Output is correct |
12 |
Correct |
94 ms |
11352 KB |
Output is correct |
13 |
Correct |
100 ms |
13880 KB |
Output is correct |
14 |
Correct |
78 ms |
9332 KB |
Output is correct |
15 |
Correct |
111 ms |
9208 KB |
Output is correct |
16 |
Correct |
106 ms |
9208 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2816 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2816 KB |
Output is correct |
27 |
Correct |
3 ms |
2816 KB |
Output is correct |
28 |
Correct |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2816 KB |
Output is correct |
31 |
Correct |
2 ms |
2816 KB |
Output is correct |
32 |
Correct |
2 ms |
2816 KB |
Output is correct |
33 |
Correct |
2 ms |
2816 KB |
Output is correct |
34 |
Correct |
2 ms |
2816 KB |
Output is correct |
35 |
Correct |
2 ms |
2816 KB |
Output is correct |
36 |
Correct |
99 ms |
11324 KB |
Output is correct |
37 |
Correct |
105 ms |
13816 KB |
Output is correct |
38 |
Correct |
71 ms |
9332 KB |
Output is correct |
39 |
Correct |
92 ms |
9208 KB |
Output is correct |
40 |
Correct |
112 ms |
9208 KB |
Output is correct |
41 |
Correct |
101 ms |
12664 KB |
Output is correct |
42 |
Correct |
114 ms |
13048 KB |
Output is correct |
43 |
Correct |
65 ms |
8564 KB |
Output is correct |
44 |
Correct |
108 ms |
9336 KB |
Output is correct |
45 |
Correct |
96 ms |
9208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2816 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2816 KB |
Output is correct |
19 |
Correct |
94 ms |
11352 KB |
Output is correct |
20 |
Correct |
100 ms |
13880 KB |
Output is correct |
21 |
Correct |
78 ms |
9332 KB |
Output is correct |
22 |
Correct |
111 ms |
9208 KB |
Output is correct |
23 |
Correct |
106 ms |
9208 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2816 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
2 ms |
2816 KB |
Output is correct |
34 |
Correct |
3 ms |
2816 KB |
Output is correct |
35 |
Correct |
2 ms |
2816 KB |
Output is correct |
36 |
Correct |
2 ms |
2816 KB |
Output is correct |
37 |
Correct |
2 ms |
2816 KB |
Output is correct |
38 |
Correct |
2 ms |
2816 KB |
Output is correct |
39 |
Correct |
2 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
2816 KB |
Output is correct |
41 |
Correct |
2 ms |
2816 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
99 ms |
11324 KB |
Output is correct |
44 |
Correct |
105 ms |
13816 KB |
Output is correct |
45 |
Correct |
71 ms |
9332 KB |
Output is correct |
46 |
Correct |
92 ms |
9208 KB |
Output is correct |
47 |
Correct |
112 ms |
9208 KB |
Output is correct |
48 |
Correct |
101 ms |
12664 KB |
Output is correct |
49 |
Correct |
114 ms |
13048 KB |
Output is correct |
50 |
Correct |
65 ms |
8564 KB |
Output is correct |
51 |
Correct |
108 ms |
9336 KB |
Output is correct |
52 |
Correct |
96 ms |
9208 KB |
Output is correct |
53 |
Correct |
109 ms |
13876 KB |
Output is correct |
54 |
Correct |
117 ms |
13048 KB |
Output is correct |
55 |
Correct |
54 ms |
8052 KB |
Output is correct |
56 |
Correct |
93 ms |
11384 KB |
Output is correct |
57 |
Correct |
89 ms |
9208 KB |
Output is correct |
58 |
Correct |
87 ms |
9208 KB |
Output is correct |
59 |
Correct |
93 ms |
9208 KB |
Output is correct |
60 |
Correct |
90 ms |
9208 KB |
Output is correct |