#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
struct Mat {
int mat[2][2] = {};
int* operator[](const int &idx) { return mat[idx]; }
Mat operator*(Mat &o) {
Mat ans;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
(ans[i][k] += ((long long)mat[i][j] * o[j][k]) % MOD) %= MOD;
return ans;
}
Mat operator^(const long long &exp) {
Mat ans = { .mat = {{1, 0}, {0, 1}} };
Mat base = *this;
for (long long e = exp; e; e >>= 1) {
if (e & 1) ans = base * ans;
base = base * base;
}
return ans;
}
};
struct Info {
int total_children, won_children, forceable_w, forceable_l;
pair<bool, int> get_data() const {
int lost_children = total_children - won_children;
if (lost_children == 0) {
return {false, forceable_w};
} else if (lost_children == 1) {
return {true, forceable_l};
} else {
return {true, 0};
}
}
void merge_child(const Info &ch) {
auto [w, f] = ch.get_data();
total_children++;
won_children += w;
if (w) forceable_w += f;
else forceable_l += f;
}
void demerge_child(const Info &ch) {
auto [w, f] = ch.get_data();
total_children--;
won_children -= w;
if (w) forceable_w -= f;
else forceable_l -= f;
}
Info() : total_children(0), won_children(0), forceable_l(0), forceable_w(1) { }
};
int solve(int N, long long D, const vector<vector<int>> &adj) {
vector<Info> dp(N);
int w = 0, l = 0;
Mat trans;
auto dfs1 = [&](auto &&dfs1, int node, int par = -1) -> void {
dp[node] = Info();
for (auto child: adj[node]) {
if (child == par) continue;
dfs1(dfs1, child, node);
dp[node].merge_child(dp[child]);
}
};
auto dfs2 = [&](auto &&dfs2, int node, int par = -1) -> void {
if (par != -1) {
dp[par].demerge_child(dp[node]);
dp[node].merge_child(dp[par]);
}
auto [ww, ff] = dp[node].get_data();
if (ww) {
w++;
(trans[0][0] += ff) %= MOD;
(trans[1][0] += N - ff) %= MOD;
(trans[1][1] += N) %= MOD;
} else {
l++;
(trans[0][0] += N - ff) %= MOD;
(trans[1][0] += ff) %= MOD;
(trans[0][1] += N) %= MOD;
}
for (auto ch: adj[node])
if (ch != par)
dfs2(dfs2, ch, node);
if (par != -1) {
dp[node].demerge_child(dp[par]);
dp[par].merge_child(dp[node]);
}
};
dfs1(dfs1, 0);
dfs2(dfs2, 0);
Mat final = trans ^ (D - 1);
int w_tot = ((long long)w * final[1][1] + (long long)l * final[1][0]) % MOD;
int l_tot = ((long long)w * final[0][1] + (long long)l * final[0][0]) % MOD;
auto [ww, ff] = dp[0].get_data();
long long ans = 0;
if (ww) {
(ans += (long long)(N - ff) * l_tot) %= MOD;
(ans += (long long)N * w_tot) %= MOD;
} else {
(ans += (long long)ff * l_tot) %= MOD;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int N; cin >> N;
long long D; cin >> D;
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int u, v; cin >> u >> v;
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
cout << solve(N, D, adj) << '\n';
}
Compilation message
startrek.cpp: In constructor 'Info::Info()':
startrek.cpp:34:52: warning: 'Info::forceable_l' will be initialized after [-Wreorder]
34 | int total_children, won_children, forceable_w, forceable_l;
| ^~~~~~~~~~~
startrek.cpp:34:39: warning: 'int Info::forceable_w' [-Wreorder]
34 | int total_children, won_children, forceable_w, forceable_l;
| ^~~~~~~~~~~
startrek.cpp:65:5: warning: when initialized here [-Wreorder]
65 | Info() : total_children(0), won_children(0), forceable_l(0), forceable_w(1) { }
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
32 ms |
11744 KB |
Output is correct |
13 |
Correct |
43 ms |
15848 KB |
Output is correct |
14 |
Correct |
23 ms |
8408 KB |
Output is correct |
15 |
Correct |
32 ms |
8540 KB |
Output is correct |
16 |
Correct |
31 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
32 ms |
11744 KB |
Output is correct |
13 |
Correct |
43 ms |
15848 KB |
Output is correct |
14 |
Correct |
23 ms |
8408 KB |
Output is correct |
15 |
Correct |
32 ms |
8540 KB |
Output is correct |
16 |
Correct |
31 ms |
8532 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
456 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
34 ms |
12236 KB |
Output is correct |
37 |
Correct |
38 ms |
16212 KB |
Output is correct |
38 |
Correct |
27 ms |
8576 KB |
Output is correct |
39 |
Correct |
30 ms |
8540 KB |
Output is correct |
40 |
Correct |
31 ms |
8540 KB |
Output is correct |
41 |
Correct |
34 ms |
14200 KB |
Output is correct |
42 |
Correct |
37 ms |
15044 KB |
Output is correct |
43 |
Correct |
21 ms |
7640 KB |
Output is correct |
44 |
Correct |
30 ms |
8540 KB |
Output is correct |
45 |
Correct |
32 ms |
8528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
456 KB |
Output is correct |
19 |
Correct |
32 ms |
11744 KB |
Output is correct |
20 |
Correct |
43 ms |
15848 KB |
Output is correct |
21 |
Correct |
23 ms |
8408 KB |
Output is correct |
22 |
Correct |
32 ms |
8540 KB |
Output is correct |
23 |
Correct |
31 ms |
8532 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
452 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
456 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
600 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
34 ms |
12236 KB |
Output is correct |
44 |
Correct |
38 ms |
16212 KB |
Output is correct |
45 |
Correct |
27 ms |
8576 KB |
Output is correct |
46 |
Correct |
30 ms |
8540 KB |
Output is correct |
47 |
Correct |
31 ms |
8540 KB |
Output is correct |
48 |
Correct |
34 ms |
14200 KB |
Output is correct |
49 |
Correct |
37 ms |
15044 KB |
Output is correct |
50 |
Correct |
21 ms |
7640 KB |
Output is correct |
51 |
Correct |
30 ms |
8540 KB |
Output is correct |
52 |
Correct |
32 ms |
8528 KB |
Output is correct |
53 |
Correct |
41 ms |
16532 KB |
Output is correct |
54 |
Correct |
35 ms |
14672 KB |
Output is correct |
55 |
Correct |
19 ms |
6872 KB |
Output is correct |
56 |
Correct |
31 ms |
12192 KB |
Output is correct |
57 |
Correct |
28 ms |
8540 KB |
Output is correct |
58 |
Correct |
29 ms |
8528 KB |
Output is correct |
59 |
Correct |
34 ms |
8576 KB |
Output is correct |
60 |
Correct |
36 ms |
8648 KB |
Output is correct |