1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
char IO; int rd(){ int s=0; while(!isdigit(IO=getchar())); do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return s; }
const int N=1e5+10,INF=1e9+10,P=1e9+7;
int n; ll m; struct Mat{ int a[2][2]; void clear(){ memset(a,0,sizeof a); } Mat operator * (const Mat x) const { Mat res; res.clear(); rep(i,0,1) rep(j,0,1) rep(k,0,1) res.a[i][k]=(res.a[i][k]+1ll*a[i][j]*x.a[j][k])%P; return res; } } Res,X;
int F[N],FC[N],FR[N],son[N][2],SR[N];
int G[N],GC[N],GR[N];
int dp[N],R[N],fa[N];
vector <int> E[N]; void dfs1(int u,int f) { fa[u]=f; for(int v:E[u]) if(v!=f) { dfs1(v,u); FC[u]+=!F[v]; if(!F[v]) { if(!son[u][0]) son[u][0]=v; else son[u][1]=v; } SR[u]+=FR[v]; } F[u]=FC[u]>0; FR[u]=!F[u]; if(FC[u]<=1) for(int v:E[u]) if(v!=f && F[u]!=F[v]) FR[u]+=FR[v]; }
void dfs2(int u,int f) { if(f) GC[u]=G[u]=!G[f],GR[u]=GR[f]+!G[u]; else GC[u]=G[u]=0,GR[u]=1; dp[u]=F[u]|G[u]; if(!dp[u]) R[u]=FR[u]+GR[u]-1; else if(FC[u]+G[u]==1) { if(G[u]) R[u]=GR[u]; else R[u]=FR[u]; } for(int v:E[u]) if(v!=f) { GC[u]=FC[u]-!F[v]+(f?!G[f]:0); G[u]=GC[u]>0; GR[u]=0; if(!G[u]) GR[u]=GR[f]+SR[u]-FR[v]+1; else if(GC[u]==1) { if(son[u][0] && son[u][0]!=v) GR[u]=FR[son[u][0]]; else if(son[u][1] && son[u][1]!=v) GR[u]=FR[son[u][1]]; else GR[u]=GR[f]; } dfs2(v,u); } }
int cnt[2]; int main(){ n=rd(),scanf("%lld",&m); rep(i,2,n) { int u=rd(),v=rd(); E[u].pb(v),E[v].pb(u); } dfs1(1,0),dfs2(1,0); rep(i,1,n) { cnt[dp[i]]++; X.a[1][dp[i]]=(X.a[1][dp[i]]+n)%P; X.a[0][!dp[i]]=(X.a[0][!dp[i]]+R[i])%P; X.a[0][dp[i]]=(X.a[0][dp[i]]+n-R[i])%P; } m--,Res.a[0][0]=Res.a[1][1]=1; while(m) { if(m&1) Res=Res*X; X=X*X; m>>=1; } int x=(1ll*Res.a[0][0]*cnt[0]+1ll*Res.a[1][0]*cnt[1])%P; int y=(1ll*Res.a[0][1]*cnt[0]+1ll*Res.a[1][1]*cnt[1])%P; int ans=0; if(!dp[1]) ans=1ll*x*R[1]%P; else ans=(1ll*x*(n-R[1])+1ll*n*y)%P; printf("%d\n",ans); }
|