#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
struct Matrix{
int n, m;
ll mat[2][2];
Matrix(){}
Matrix(int n, int m): n(n), m(m){
for(int i=0; i<n; i++) for(int j=0; j<m; j++) mat[i][j] = 0;
}
Matrix operator*(const Matrix &r)const{
assert(m == r.n);
Matrix ret (n, r.m);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
for(int k=0; k<r.m; k++){
ret.mat[i][k] += mat[i][j] * r.mat[j][k];
ret.mat[i][k] %= MOD;
}
}
}
return ret;
}
};
ll mpow(ll x, ll y){
if(!y) return 1;
if(y%2) return mpow(x, y-1) * x % MOD;
ll tmp = mpow(x, y/2);
return tmp*tmp%MOD;
}
Matrix BasicMatrix(int n, int m){
Matrix mat (n, m);
assert(n==m);
for(int i=0; i<n; i++) mat.mat[i][i] = 1;
return mat;
}
Matrix mpow(Matrix x, ll y){
if(!y) return BasicMatrix(x.n, x.m);
if(y&1) return mpow(x, y-1) * x;
Matrix tmp = mpow(x, y/2);
return tmp * tmp;
}
int n, lim;
ll k;
vector<int> link[100002][2];
bool mat[300002];
int ey[300002];
int parEdge[100002];
bool canWin[100002];
bool dfsChk[300002];
int group[100002], groupCnt[2];
int res[100002];
ll ans;
bool groupVisited[100002];
void groupDfs(int x, int p=-1){
groupCnt[group[x]]++;
groupVisited[x] = 1;
for(auto y: link[x][0]){
if(ey[y]==p || groupVisited[ey[y]]) continue;
group[ey[y]] = !group[x];
groupDfs(ey[y], x);
}
}
int winCnt[100002];
bool dfs(int pe){
if(dfsChk[pe]) return mat[pe];
int x = ey[pe];
mat[pe] = !!(winCnt[x] - (pe > lim ? 0 : (dfsChk[pe^1] && !mat[pe^1])));
dfsChk[pe] = 1;
vector<int> nLink;
for(auto y: link[x][0]){
if((y^pe)==1) nLink.push_back(y);
else if(!dfs(y)) winCnt[x]++, mat[pe] = 1;
}
link[x][0].swap(nLink);
return mat[pe];
}
int find1[300002][2];
int findV[100002][2];
bool findChk[2][300002];
int dfsFind(int pe, bool dp){ /// 무조건 지나야 하는 상대 차례 점 개수 찾기
if(findChk[dp][pe]) return find1[pe][dp];
int x = ey[pe];
findChk[dp][pe] = 1;
if(!dp){ /// my turn
int cnt = 0;
for(auto y: link[x][0]){
if((y^1)!=pe && !mat[y]) cnt++;
}
if(cnt > 1) return find1[pe][dp] = 0;
assert(cnt == 1);
for(auto y: link[x][0]){
if((y^1)!=pe && !mat[y]) return find1[pe][dp] = dfsFind(y, !dp);
}
exit(1);
}
else{ /// your turn
int ret = 1 + findV[x][1] - ((pe>lim || !findChk[0][pe^1]) ? 0 : find1[pe^1][0]);
vector<int> v;
for(auto y: link[x][1]){
if((y^1)==pe) v.push_back(y);
else{
int tmp = dfsFind(y, !dp);
ret += tmp, findV[x][1] += tmp;
}
}
link[x][1].swap(v);
return find1[pe][dp] = ret;
}
}
int main(){
// freopen("input.txt", "r", stdin);
scanf("%d %lld", &n, &k);
lim = (n-1)*2-1;
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x][0].push_back(i*2-2);
link[x][1].push_back(i*2-2);
link[y][0].push_back(i*2-1);
link[y][1].push_back(i*2-1);
ey[i*2-2] = y;
ey[i*2-1] = x;
}
for(int i=1; i<=n; i++){
ey[lim+i] = i;
parEdge[i] = lim+i;
}
groupDfs(1);
for(int i=1; i<=n; i++){
canWin[i] = dfs(parEdge[i]);
}
for(int i=1; i<=n; i++) link[i][0] = link[i][1];
for(int i=1; i<=n; i++){
if(canWin[i]) res[i] = groupCnt[!group[i]] - dfsFind(parEdge[i], 0);
else res[i] = dfsFind(parEdge[i], 1);
}
Matrix first (1, 2);
first.mat[0][0] = first.mat[0][1] = 0;
for(int i=1; i<=n; i++){
if(canWin[i]) first.mat[0][0]++;
else first.mat[0][1]++;
}
Matrix multiplier (2, 2); /// 0: WIN, 1: LOSE
for(int i=1; i<=n; i++){
if(canWin[i]){
multiplier.mat[0][0] = (multiplier.mat[0][0] + n) % MOD;
multiplier.mat[1][0] = (multiplier.mat[1][0] + groupCnt[group[i]]) % MOD;
multiplier.mat[1][0] = (multiplier.mat[1][0] + res[i]) % MOD;
multiplier.mat[1][1] = (multiplier.mat[1][1] + n - groupCnt[group[i]] - res[i] + MOD)%MOD;
}
else{
multiplier.mat[1][0] = (multiplier.mat[1][0] + res[i]) % MOD;
multiplier.mat[0][1] = (multiplier.mat[0][1] + n) % MOD;
multiplier.mat[1][1] = (multiplier.mat[1][1] + n - res[i]) % MOD;
}
}
multiplier = mpow(multiplier, k-1);
first = first * multiplier;
ll Wsum = first.mat[0][0], Lsum = first.mat[0][1];
ll ans = 0;
if(canWin[1]){
ans = (Wsum * n + Lsum * groupCnt[group[1]] + Lsum * res[1]) % MOD;
}
else ans = Lsum * res[1] % MOD;
printf("%lld", ans);
}
Compilation message
startrek.cpp: In function 'int main()':
startrek.cpp:127:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %lld", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~
startrek.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
229 ms |
25008 KB |
Output is correct |
13 |
Correct |
208 ms |
32472 KB |
Output is correct |
14 |
Correct |
150 ms |
18592 KB |
Output is correct |
15 |
Correct |
231 ms |
18508 KB |
Output is correct |
16 |
Correct |
209 ms |
18628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
5216 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5184 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
229 ms |
25008 KB |
Output is correct |
13 |
Correct |
208 ms |
32472 KB |
Output is correct |
14 |
Correct |
150 ms |
18592 KB |
Output is correct |
15 |
Correct |
231 ms |
18508 KB |
Output is correct |
16 |
Correct |
209 ms |
18628 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
4948 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5216 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5184 KB |
Output is correct |
32 |
Correct |
3 ms |
5332 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5204 KB |
Output is correct |
35 |
Correct |
3 ms |
5204 KB |
Output is correct |
36 |
Correct |
202 ms |
25016 KB |
Output is correct |
37 |
Correct |
248 ms |
32436 KB |
Output is correct |
38 |
Correct |
209 ms |
18708 KB |
Output is correct |
39 |
Correct |
185 ms |
18512 KB |
Output is correct |
40 |
Correct |
183 ms |
18660 KB |
Output is correct |
41 |
Correct |
237 ms |
29088 KB |
Output is correct |
42 |
Correct |
199 ms |
30088 KB |
Output is correct |
43 |
Correct |
140 ms |
17108 KB |
Output is correct |
44 |
Correct |
187 ms |
18632 KB |
Output is correct |
45 |
Correct |
164 ms |
18600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5044 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
229 ms |
25008 KB |
Output is correct |
20 |
Correct |
208 ms |
32472 KB |
Output is correct |
21 |
Correct |
150 ms |
18592 KB |
Output is correct |
22 |
Correct |
231 ms |
18508 KB |
Output is correct |
23 |
Correct |
209 ms |
18628 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
2 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
4948 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5204 KB |
Output is correct |
34 |
Correct |
3 ms |
5332 KB |
Output is correct |
35 |
Correct |
3 ms |
5216 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5184 KB |
Output is correct |
39 |
Correct |
3 ms |
5332 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5204 KB |
Output is correct |
42 |
Correct |
3 ms |
5204 KB |
Output is correct |
43 |
Correct |
202 ms |
25016 KB |
Output is correct |
44 |
Correct |
248 ms |
32436 KB |
Output is correct |
45 |
Correct |
209 ms |
18708 KB |
Output is correct |
46 |
Correct |
185 ms |
18512 KB |
Output is correct |
47 |
Correct |
183 ms |
18660 KB |
Output is correct |
48 |
Correct |
237 ms |
29088 KB |
Output is correct |
49 |
Correct |
199 ms |
30088 KB |
Output is correct |
50 |
Correct |
140 ms |
17108 KB |
Output is correct |
51 |
Correct |
187 ms |
18632 KB |
Output is correct |
52 |
Correct |
164 ms |
18600 KB |
Output is correct |
53 |
Correct |
221 ms |
32376 KB |
Output is correct |
54 |
Correct |
240 ms |
29644 KB |
Output is correct |
55 |
Correct |
114 ms |
15924 KB |
Output is correct |
56 |
Correct |
178 ms |
25036 KB |
Output is correct |
57 |
Correct |
204 ms |
18788 KB |
Output is correct |
58 |
Correct |
167 ms |
18740 KB |
Output is correct |
59 |
Correct |
196 ms |
18532 KB |
Output is correct |
60 |
Correct |
197 ms |
18644 KB |
Output is correct |