#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mx = 1e5 + 5;
int sz[mx]{};
int cnt2[mx]{};
int win2[mx]{};
int cnt[mx]{};
int win[mx]{};
bool vis[mx]{};
vector<int> adj[mx]{};
int dfs(int v, int p) {
if (vis[v]) return 0;
sz[v] = 1;
for (auto& x : adj[v])
if (x != p)
sz[v] += dfs(x, v);
return sz[v];
}
void dfs2(int v, int p) {
//if all lead to win, it loses
if (vis[v]) return;
int cntlose = 0;
int a = 0, b = 0;
for (auto& x : adj[v]) {
if (x == p) continue;
dfs2(x, v);
if (win2[x]) a += cnt2[x];
else b += cnt2[x];
cntlose += !win2[x];
}
// cout << "a" << cntlose << "," << v << "," << a << "," << b << endl;
cnt2[v] = 0;
if (cntlose == 1) {
cnt2[v] = b;
}
if (cntlose == 0) {
cnt2[v] = a + 1;
}
win2[v] = cntlose ? 1 : 0;
}
void cd(int v) {
dfs(v, -1);
int N = sz[v];
int p = v;
bool cont = 1;
while (cont) {
cont = 0;
for (auto& x : adj[v]) {
if (sz[x] >= N / 2 && !vis[x] && x != p) {
p = v; v = x;
cont = 1;
break;
}
}
}
cnt2[v] = 0, win2[v] = 0;
vis[v] = 1;
int cntlose = 0, a = 0, b = 0;
for (auto& x : adj[v]) {
dfs2(x, v);
if (win2[x]) a += cnt2[x];
else b += cnt2[x];
cntlose += !win2[x];
}
for (auto& x : adj[v]) {
if (vis[x]) continue;
dfs2(x, v);
if (win2[x]) a -= cnt2[x];
else b -= cnt2[x];
cntlose -= !win2[x];
// cout << v << "," << cntlose << "," << a << "," << b << endl;
cnt2[v] = 0;
if (cntlose == 1) {
cnt2[v] = b;
}
if (cntlose == 0) {
cnt2[v] = a + 1;
}
win2[v] = cntlose ? 1 : 0;
cd(x);
dfs2(x, v);
if (win2[x]) a += cnt2[x];
else b += cnt2[x];
cntlose += !win2[x];
}
// cout << "fin: " << v << "," << cntlose << "," << a << "," << b << endl;
cnt[v] = 0;
if (cntlose == 1) {
cnt[v] = b;
}
if (cntlose == 0) {
cnt[v] = a + 1;
}
win[v] = cntlose ? 1 : 0;
vis[v] = 0;
}
const int mod = 1e9 + 7;
struct uwu {
int a = 0, b = 0, c = 0, d = 0;
};
uwu combine(uwu a, uwu b) {
uwu res;
res.a = (a.a * b.a + a.c * b.c) % mod;
res.b = (a.a * b.b + a.c * b.d + a.b * (b.a + b.b + b.c + b.d)) % mod;
res.c = (a.c * b.a + a.a * b.c) % mod;
res.d = (a.c * b.b + a.a * b.d + a.d * (b.a + b.b + b.c + b.d)) % mod;
return res;
}
uwu binpow(uwu a, int b) {
if (b == 1) return a;
uwu c = binpow(combine(a, a), b / 2);
if (b & 1) c = combine(c, a);
return c;
}
void solve() {
int N, D; cin >> N >> D;
for (int i = 0; i < N - 1; i++) {
int a, b; cin >> a >> b; a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
cd(0);
int cntw = 0, cntl = 0;
uwu x;
for (int i = 0; i < N; i++) {
if (win[i]) {
cntw++;
x.a += cnt[i];
x.b += N - cnt[i];
} else {
cntl++;
x.c += cnt[i];
x.d += N - cnt[i];
}
}
cntl %= mod; cntw %= mod; x.a %= mod; x.b %= mod; x.c %= mod; x.d %= mod;
if (D == 0) {
cout << win[0] << endl;
} else if (D == 1) {
uwu v;
if (win[0]) {
v.a += cnt[0];
v.b += N - cnt[0];
} else {
v.c += cnt[0];
v.d += N - cnt[0];
}
// cout << v.c << "," << cntl << endl;
cout << (v.a * cntw + v.b * N + v.c * cntl) % mod << endl;
} else {
uwu v;
if (win[0]) {
v.a += cnt[0];
v.b += N - cnt[0];
} else {
v.c += cnt[0];
v.d += N - cnt[0];
}
v = combine(v, binpow(x, D - 1));
cout << (v.a * cntw + v.b * N + v.c * cntl) % mod << endl;
}
}
int32_t main() {
int t = 1;
//cin >> t;
while (t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 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 |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 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 |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
239 ms |
15672 KB |
Output is correct |
13 |
Correct |
281 ms |
17072 KB |
Output is correct |
14 |
Correct |
56 ms |
11124 KB |
Output is correct |
15 |
Correct |
140 ms |
11272 KB |
Output is correct |
16 |
Correct |
164 ms |
11468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 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 |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 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 |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2784 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 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 |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 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 |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
239 ms |
15672 KB |
Output is correct |
13 |
Correct |
281 ms |
17072 KB |
Output is correct |
14 |
Correct |
56 ms |
11124 KB |
Output is correct |
15 |
Correct |
140 ms |
11272 KB |
Output is correct |
16 |
Correct |
164 ms |
11468 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 |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2784 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
246 ms |
15680 KB |
Output is correct |
37 |
Correct |
268 ms |
17080 KB |
Output is correct |
38 |
Correct |
55 ms |
11084 KB |
Output is correct |
39 |
Correct |
158 ms |
11300 KB |
Output is correct |
40 |
Correct |
144 ms |
11304 KB |
Output is correct |
41 |
Correct |
223 ms |
15708 KB |
Output is correct |
42 |
Correct |
218 ms |
15932 KB |
Output is correct |
43 |
Correct |
50 ms |
10148 KB |
Output is correct |
44 |
Correct |
141 ms |
11388 KB |
Output is correct |
45 |
Correct |
141 ms |
11204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 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 |
2772 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 |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
239 ms |
15672 KB |
Output is correct |
20 |
Correct |
281 ms |
17072 KB |
Output is correct |
21 |
Correct |
56 ms |
11124 KB |
Output is correct |
22 |
Correct |
140 ms |
11272 KB |
Output is correct |
23 |
Correct |
164 ms |
11468 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 |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
1 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2784 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2644 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
246 ms |
15680 KB |
Output is correct |
44 |
Correct |
268 ms |
17080 KB |
Output is correct |
45 |
Correct |
55 ms |
11084 KB |
Output is correct |
46 |
Correct |
158 ms |
11300 KB |
Output is correct |
47 |
Correct |
144 ms |
11304 KB |
Output is correct |
48 |
Correct |
223 ms |
15708 KB |
Output is correct |
49 |
Correct |
218 ms |
15932 KB |
Output is correct |
50 |
Correct |
50 ms |
10148 KB |
Output is correct |
51 |
Correct |
141 ms |
11388 KB |
Output is correct |
52 |
Correct |
141 ms |
11204 KB |
Output is correct |
53 |
Correct |
252 ms |
17184 KB |
Output is correct |
54 |
Correct |
248 ms |
15956 KB |
Output is correct |
55 |
Correct |
44 ms |
9476 KB |
Output is correct |
56 |
Correct |
195 ms |
14108 KB |
Output is correct |
57 |
Correct |
135 ms |
11484 KB |
Output is correct |
58 |
Correct |
135 ms |
11468 KB |
Output is correct |
59 |
Correct |
135 ms |
11264 KB |
Output is correct |
60 |
Correct |
143 ms |
11144 KB |
Output is correct |