#include<bits/stdc++.h>
//#include<iostream>
//#include<vector>
using namespace std;
#define int long long
const int mod=1e9+7;
// Using a modint type instead of inserting "% mod" in appropriate
// places makes it easier to avoid both negative numbers and overflows.
struct modint {
int val;
modint() : val(0) {}
modint(int val_) : val(val_ % mod) {
if (val < 0) val += mod;
}
};
modint operator+(modint a, modint b) {
return a.val + b.val;
}
modint operator-(modint a, modint b) {
return a.val - b.val;
}
modint operator*(modint a, modint b) {
return a.val * b.val;
}
// Fixed-size arrays are nicer than vectors in that you don't
// need to resize(), and they also work much faster, so if we can
// use them, it's typically a good idea.
using vect = array<modint, 2>;
using matrix = array<vect, 2>;
matrix unit() {
// Note that " = {};" is important to zero-initialize std::array.
matrix res = {};
for (int i = 0; i < res.size(); ++i) res[i][i] = 1;
return res;
}
vect mul(matrix a, vect b) {
vect res = {};
for (int i = 0; i < res.size(); ++i) {
for (int j = 0; j < res.size(); ++j) {
res[i] = res[i] + a[i][j] * b[j];
}
}
return res;
}
matrix multiply(matrix a, matrix b) {
matrix res = {};
for (int i = 0; i < res.size(); ++i) {
for (int j = 0; j < res.size(); ++j) {
for (int k = 0; k < res.size(); ++k) {
res[i][j] = res[i][j] + a[i][k] * b[k][j];
}
}
}
return res;
}
matrix power(matrix a, int e){
if(e==0){
return unit();
}
matrix res=power(a, e/2);
res=multiply(res, res);
if(e%2==1){
res=multiply(res, a);
}
return res;
}
bool dfs_win(int v, int peid, const vector<vector<int> >& adi, const vector<int>& dest, vector<int>& vertex_status, vector<vector<int>>& red_neighbors){
if (vertex_status[v] == -2) {
vertex_status[v] = peid;
for(auto eid: adi[v]){
if (eid == peid) continue;
auto u = dest[eid];
if(!dfs_win(u, eid ^ 1, adi, dest, vertex_status, red_neighbors)){
red_neighbors[v].push_back(eid);
}
}
} else {
if (vertex_status[v] >= 0 && vertex_status[v] != peid) {
auto eid = vertex_status[v];
auto u = dest[eid];
if(!dfs_win(u, eid ^ 1, adi, dest, vertex_status, red_neighbors)){
red_neighbors[v].push_back(eid);
}
vertex_status[v] = -1;
}
}
if (vertex_status[v] == peid) {
return red_neighbors[v].size() > 0;
} else {
if (red_neighbors[v].size() >= 2) return true;
if (red_neighbors[v].size() == 0) return false;
return red_neighbors[v][0] != peid;
}
}
int dfs_redforce(int v, int peid, const vector<vector<int> >& adi, const vector<int>& dest, const vector<vector<int>>& red_neighbors, vector<int>& vertex_status, vector<int>& redf, vector<int>& summe){
bool is_green;
if (red_neighbors[v].size() >= 2) is_green = true;
else if (red_neighbors[v].size() == 0) is_green = false;
else is_green = red_neighbors[v][0] != peid;
if(vertex_status[v]==-2){
for(auto eid: adi[v]){
if(eid==peid) {
continue;
}
auto u = dest[eid];
redf[eid]=dfs_redforce(u, eid ^ 1, adi, dest, red_neighbors, vertex_status, redf, summe);
summe[v]+=redf[eid];
}
vertex_status[v]=peid;
}
else{
if(vertex_status[v]>=0 && vertex_status[v]!=peid){
auto eid = vertex_status[v];
auto u = dest[eid];
redf[eid]=dfs_redforce(u, eid ^ 1, adi, dest, red_neighbors, vertex_status, redf, summe);
summe[v]+=redf[eid];
vertex_status[v]=-1;
}
}
int redforce = 0;
if(!is_green){ //red
redforce++;
redforce+=summe[v];
if (vertex_status[v] == -1 && peid != -1) {
redforce-=redf[peid];
}
}
else { //green with one red child
bool needed=red_neighbors[v].size()<3;
int u=red_neighbors[v][0];
if(red_neighbors[v].size()==2){
if(red_neighbors[v][0]==peid) u=red_neighbors[v][1];
else if(red_neighbors[v][1]!=peid) needed=false;
}
if (needed) {
redforce+=redf[u];
}
}
return redforce;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input1.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, d;
cin >> n >> d;
// Now we store edge numbers here, and the target of the edge in "dest" array.
// This allows us to avoid unordered_map. We also maintain the property
// that the reverse to the edge x is the edge x^1.
vector<vector<int> > adi(n);
vector<int> dest(2 * (n - 1));
for(int i=0; i<n-1; i++){
int a,b;
cin >> a >> b;
adi[a-1].push_back(2 * i);
dest[2 * i] = b - 1;
adi[b-1].push_back(2 * i + 1);
dest[2 * i + 1] = a - 1;
}
int G=0; int R=0;
int g1=0; int r1=0;
int c=0;
vector<int> vertex_status(n, -2);
vector<int> vertex_status2(n, -2);
vector<vector<int>> red_neighbors(n);
vector<int> redf(2 * (n - 1), 0);
vector<int> summe(n, 0);
for(int i=n-1; i>=0; i--){
bool we_win=dfs_win(i, -1, adi, dest, vertex_status, red_neighbors);
int redforce=dfs_redforce(i, -1, adi, dest, red_neighbors, vertex_status2, redf, summe);
if(we_win){
c++;
g1=n;
r1=n-redforce;
}
else{
g1=0;
r1=redforce;
}
G+=g1;
R+=r1;
}
matrix m1 = {};
m1[0][0] = g1;
m1[0][1] = r1;
m1[1][0] = n - g1; // This value is not important since we only print ans[0], but in case we also wanted ans[1] this would be correct.
m1[1][1] = n - r1; // This value is not important since we only print ans[0], but in case we also wanted ans[1] this would be correct.
matrix m2 = {};
m2[0][0] = G;
m2[0][1] = R;
m2[1][0] = n * n - G;
m2[1][1] = n * n - R;
matrix p=power(m2, d-1);
vect v=mul(p, {c, n-c});
vect ans=mul(m1, v);
cout << ans[0].val << "\n";
}
Compilation message
startrek.cpp: In function 'matrix unit()':
startrek.cpp:42:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<std::array<modint, 2>, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < res.size(); ++i) res[i][i] = 1;
| ~~^~~~~~~~~~~~
startrek.cpp: In function 'vect mul(matrix, vect)':
startrek.cpp:48:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<modint, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < res.size(); ++i) {
| ~~^~~~~~~~~~~~
startrek.cpp:49:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<modint, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int j = 0; j < res.size(); ++j) {
| ~~^~~~~~~~~~~~
startrek.cpp: In function 'matrix multiply(matrix, matrix)':
startrek.cpp:58:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<std::array<modint, 2>, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < res.size(); ++i) {
| ~~^~~~~~~~~~~~
startrek.cpp:59:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<std::array<modint, 2>, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int j = 0; j < res.size(); ++j) {
| ~~^~~~~~~~~~~~
startrek.cpp:60:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::array<std::array<modint, 2>, 2>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int k = 0; k < res.size(); ++k) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
155 ms |
25420 KB |
Output is correct |
13 |
Correct |
126 ms |
30700 KB |
Output is correct |
14 |
Correct |
94 ms |
15552 KB |
Output is correct |
15 |
Correct |
138 ms |
16784 KB |
Output is correct |
16 |
Correct |
106 ms |
15824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
232 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
496 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
155 ms |
25420 KB |
Output is correct |
13 |
Correct |
126 ms |
30700 KB |
Output is correct |
14 |
Correct |
94 ms |
15552 KB |
Output is correct |
15 |
Correct |
138 ms |
16784 KB |
Output is correct |
16 |
Correct |
106 ms |
15824 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
232 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
496 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
127 ms |
25364 KB |
Output is correct |
37 |
Correct |
121 ms |
30708 KB |
Output is correct |
38 |
Correct |
81 ms |
15580 KB |
Output is correct |
39 |
Correct |
139 ms |
16844 KB |
Output is correct |
40 |
Correct |
124 ms |
15788 KB |
Output is correct |
41 |
Correct |
108 ms |
22780 KB |
Output is correct |
42 |
Correct |
107 ms |
26240 KB |
Output is correct |
43 |
Correct |
65 ms |
13724 KB |
Output is correct |
44 |
Correct |
150 ms |
16568 KB |
Output is correct |
45 |
Correct |
101 ms |
15560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
155 ms |
25420 KB |
Output is correct |
20 |
Correct |
126 ms |
30700 KB |
Output is correct |
21 |
Correct |
94 ms |
15552 KB |
Output is correct |
22 |
Correct |
138 ms |
16784 KB |
Output is correct |
23 |
Correct |
106 ms |
15824 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
232 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
496 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
127 ms |
25364 KB |
Output is correct |
44 |
Correct |
121 ms |
30708 KB |
Output is correct |
45 |
Correct |
81 ms |
15580 KB |
Output is correct |
46 |
Correct |
139 ms |
16844 KB |
Output is correct |
47 |
Correct |
124 ms |
15788 KB |
Output is correct |
48 |
Correct |
108 ms |
22780 KB |
Output is correct |
49 |
Correct |
107 ms |
26240 KB |
Output is correct |
50 |
Correct |
65 ms |
13724 KB |
Output is correct |
51 |
Correct |
150 ms |
16568 KB |
Output is correct |
52 |
Correct |
101 ms |
15560 KB |
Output is correct |
53 |
Correct |
115 ms |
27352 KB |
Output is correct |
54 |
Correct |
116 ms |
25820 KB |
Output is correct |
55 |
Correct |
57 ms |
12460 KB |
Output is correct |
56 |
Correct |
115 ms |
21356 KB |
Output is correct |
57 |
Correct |
106 ms |
16192 KB |
Output is correct |
58 |
Correct |
120 ms |
15988 KB |
Output is correct |
59 |
Correct |
105 ms |
16928 KB |
Output is correct |
60 |
Correct |
106 ms |
15468 KB |
Output is correct |