This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
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;
}
vector<int> cnt_red_children;
vector<int> redchild;
bool dfs_win(int v, int p, vector<vector<int> >& adi){
for(auto u: adi[v]){
if(u==p) continue;
if(!dfs_win(u, v, adi)){
redchild[v]=u;
cnt_red_children[v]++;
}
}
return cnt_red_children[v]>0;
}
/*vector<bool> win_up;
int lose=0; //=n-c
void dfs_win2(int v, int p, vector<vector<int> >& adi){
if(p==-1){
win_up[v]=false;
}
else if(cnt_red_children[v]){
if(cnt_red_children[p]==0 && !win_up[p]){
win_up[v]=true;
}
else{
win_up[v]=false;
}
}
else if([v]==0){
if(win_down[p]==1 && !win_up[p]){
win_up[v]=true;
}
else{
win_up[v]=false;
}
}
if(!win_up[v] && !win_down[v]){
lose++;
}
for(auto u: adi[v]){
if(u!=p) dfs_win2(u, v, adi);
}
}*/
int redforce=0;
void dfs_redforce(int v, int p, vector<vector<int> >& adi){
if(cnt_red_children[v]==0){ //red
redforce++;
for(auto u: adi[v]){
if(u!=p) dfs_redforce(u, v, adi);
}
}
else if(cnt_red_children[v]==1){ //green with one red child
dfs_redforce(redchild[v], v, adi);
}
}
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--){
redforce=0;
//win_up.assign(n, 0);
redchild.assign(n, -1);
cnt_red_children.assign(n, 0);
bool we_win=dfs_win(i, -1, adi);
//dfs_win2(i, -1, adi);
dfs_redforce(i, -1, adi);
if(we_win){
c++;
g1=n;
r1=n-redforce;
}
else{
g1=0;
r1=redforce;
}
G+=g1;
R+=r1;
}
//cout << "redforce: " << redforce << "\n";
//cout << g1 << " " << r1 << " " << c << "\n";
matrix m1={g1, r1, 0, 0};
matrix m2={G, R, n*n-G, n*n-R};
//matrix m2={G, n*n-G, R, n*n-R};
matrix p=power(m2, d-1);
vector<int> ans=mul(m1, mul(p, {c, n-c}));
cout << ans[0] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |