#include<bits/stdc++.h>
//#include<iostream>
//#include<vector>
using namespace std;
#define int long long
const int mod=1e9+7;
struct matrix{
int a=1;
int b=0;
int c=0;
int d=1;
};
matrix zero={1, 0, 0, 1};
vector<int> mul(matrix a, vector<int> b){
vector<int> ans(2, -1);
ans[0]=(a.a*b[0])%mod +(a.b*b[1])%mod;
ans[0]%=mod;
ans[1]=(a.c*b[0])%mod +(a.d*b[1])%mod;
ans[1]%=mod;
return ans;
}
matrix multiply(matrix a, matrix b){
matrix ans;
ans.a=(a.a*b.a)%mod + (a.b*b.c)%mod;
ans.a%=mod;
ans.b=(a.a*b.b)%mod + (a.b*b.d)%mod;
ans.b%=mod;
ans.c=(a.c*b.a)%mod + (a.d*b.c)%mod;
ans.c%=mod;
ans.d=(a.c*b.b)%mod + (a.d*b.d)%mod;
ans.d%=mod;
return ans;
}
matrix power(matrix a, int e){
if(e==0){
return zero;
}
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 p, vector<vector<int> >& adi, vector<int>& cnt_red_children, vector<int>& redchild){
for(auto u: adi[v]){
if(u==p) continue;
if(!dfs_win(u, v, adi, cnt_red_children, redchild)){
redchild[v]=u;
cnt_red_children[v]++;
}
}
return cnt_red_children[v]>0;
}
int dfs_redforce(int v, int p, vector<vector<int> >& adi, vector<int>& cnt_red_children, vector<int>& redchild){
int redforce=0;
if(cnt_red_children[v]==0){ //red
redforce++;
for(auto u: adi[v]){
if(u!=p) redforce+=dfs_redforce(u, v, adi, cnt_red_children, redchild);
}
}
else if(cnt_red_children[v]==1){ //green with one red child
redforce+=dfs_redforce(redchild[v], v, adi, cnt_red_children, redchild);
}
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;
vector<vector<int> > adi(n);
for(int i=0; i<n-1; i++){
int a,b;
cin >> a >> b;
adi[a-1].push_back(b-1);
adi[b-1].push_back(a-1);
}
int G=0; int R=0;
int g1=0; int r1=0;
int c=0;
for(int i=n-1; i>=0; i--){
vector<int> redchild(n, -1);
vector<int> cnt_red_children(n, 0);
bool we_win=dfs_win(i, -1, adi, cnt_red_children, redchild);
int redforce=dfs_redforce(i, -1, adi, cnt_red_children, redchild);
if(we_win){
c++;
g1=n;
r1=n-redforce;
}
else{
g1=0;
r1=redforce;
}
//cout << i+1 << ": " << g1 << " " << r1 << " " << redforce << "\n";
G+=g1; G%=mod;
R+=r1; R%=mod;
}
//cout << g1 << " " << r1 << " " << c << "\n";
//cout << G << " " << R << "\n";
matrix m1={g1, r1, 0, 0};
matrix m2={G, R, ((n*n)-G)%mod, ((n*n)-R)%mod};
//cout << m2.a << " " << m2.b << "\n" << m2.c << " " << m2.d << "\n";
matrix p=power(m2, d-1);
//cout << p.a << " " << p.b << "\n" << p.c << " " << p.d << "\n";
vector<int> v=mul(p, {c, n-c});
//cout << v[0] << " " << v[1] << "\n";
vector<int> ans=mul(m1, v);
cout << ans[0] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
13 ms |
408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
20 ms |
340 KB |
Output is correct |
8 |
Correct |
27 ms |
488 KB |
Output is correct |
9 |
Correct |
14 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
404 KB |
Output is correct |
11 |
Correct |
20 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
20 ms |
340 KB |
Output is correct |
8 |
Correct |
27 ms |
488 KB |
Output is correct |
9 |
Correct |
14 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
404 KB |
Output is correct |
11 |
Correct |
20 ms |
340 KB |
Output is correct |
12 |
Execution timed out |
1075 ms |
16068 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
20 ms |
340 KB |
Output is correct |
8 |
Correct |
27 ms |
488 KB |
Output is correct |
9 |
Correct |
14 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
404 KB |
Output is correct |
11 |
Correct |
20 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
14 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
324 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
19 ms |
340 KB |
Output is correct |
22 |
Correct |
24 ms |
468 KB |
Output is correct |
23 |
Correct |
12 ms |
412 KB |
Output is correct |
24 |
Correct |
13 ms |
400 KB |
Output is correct |
25 |
Correct |
18 ms |
340 KB |
Output is correct |
26 |
Correct |
15 ms |
424 KB |
Output is correct |
27 |
Correct |
25 ms |
448 KB |
Output is correct |
28 |
Correct |
8 ms |
324 KB |
Output is correct |
29 |
Correct |
14 ms |
340 KB |
Output is correct |
30 |
Correct |
17 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
20 ms |
340 KB |
Output is correct |
8 |
Correct |
27 ms |
488 KB |
Output is correct |
9 |
Correct |
14 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
404 KB |
Output is correct |
11 |
Correct |
20 ms |
340 KB |
Output is correct |
12 |
Execution timed out |
1075 ms |
16068 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
13 ms |
408 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
20 ms |
340 KB |
Output is correct |
15 |
Correct |
27 ms |
488 KB |
Output is correct |
16 |
Correct |
14 ms |
340 KB |
Output is correct |
17 |
Correct |
14 ms |
404 KB |
Output is correct |
18 |
Correct |
20 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
1075 ms |
16068 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |