1 solutions

  • 0
    @ 2026-7-29 10:59:37
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                                  ޱ                   BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         1000000007
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    struct matrix{
    	int a,b,c,d;
    	matrix operator *(matrix e){
    		return {(a*e.a+b*e.c)%MOD,(a*e.b+b*e.d)%MOD,(c*e.a+d*e.c)%MOD,(c*e.b+d*e.d)%MOD};
    	}
    	matrix qpow(int e){
    		matrix f=*this,res={1,0,0,1};
    		while(e){
    			if(e&1)res=res*f;
    			f=f*f;e>>=1;
    		}
    		return res;
    	}
    	void output(){
    		cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
    	}
    }M;
    int n,d;
    vector<int>v[100005];
    int f[100005],g[100005];
    int res[100005];
    int dp[100005][2][2];// Ƿ  й   ȥ 㣬 Ƿ ʤ  
    int dp2[100005][2][2];
    void dfs(int x,int pre){
    	for(auto i:v[x])if(i!=pre)dfs(i,x),f[x]|=!f[i];
    }
    void dfs2(int x,int pre){
    	int t=!g[x];
    	for(auto i:v[x])if(i!=pre)t+=!f[i];
    	for(auto i:v[x])if(i!=pre)g[i]=(t-!f[i])>0,dfs2(i,x);
    }
    void dfs3(int x,int pre){
    	dp[x][0][0]=dp[x][1][1]=1;//    ȥ  /Ӯ
    	for(auto i:v[x])if(i!=pre){
    		dfs3(i,x);
    		int f[2][2];
    		REP(j,0,2)REP(l,0,2)f[j][l]=dp[x][j][l],dp[x][j][l]=0;
    		REP(j,0,2){
    			REP(l,0,2)if(f[j][l]){
    				REP(j2,0,2)if(!j||!j2){
    					REP(l2,0,2)if(dp[i][j2][l2]){
    						(dp[x][j|j2][l|(!l2)]+=f[j][l]*dp[i][j2][l2])%=MOD;
    					}
    				}
    			}
    		}
    	}
    }
    void dfs4(int x,int pre){
    	int m=v[x].size()-(x>0);
    	vector<vector<vector<int>>>fr(m+1,vector<vector<int>>(2,vector<int>(2,0))),bk=fr;
    	if(x){
    		REP(i,0,2)REP(j,0,2){
    			(fr[0][i][!j]+=dp2[x][i][j])%=MOD;
    			if(!i)(fr[0][1][1]+=dp2[x][i][j])%=MOD;
    		}
    	}else fr[0][0][0]=1,fr[0][1][1]=1;
    	bk[m][0][0]=1;
    	vector<int>t;for(auto i:v[x])if(i!=pre)t.pb(i);
    	REP(i,0,m){
    		int p=t[i];
    		REP(j,0,2)REP(l,0,2)if(fr[i][j][l]){
    			REP(j2,0,2)if(!j||!j2)REP(l2,0,2)if(dp[p][j2][l2]){
    				(fr[i+1][j|j2][l|(!l2)]+=fr[i][j][l]*dp[p][j2][l2])%=MOD;
    			}
    		}
    	}
    	for(int i=m-1;i>=0;--i){
    		int p=t[i];
    		REP(j,0,2)REP(l,0,2)if(bk[i+1][j][l]){
    			REP(j2,0,2)if(!j||!j2)REP(l2,0,2)if(dp[p][j2][l2]){
    				(bk[i][j|j2][l|(!l2)]+=bk[i+1][j][l]*dp[p][j2][l2])%=MOD;
    			}
    		}
    	}
    	REP(i,0,m){
    		int p=t[i];
    		REP(j,0,2)REP(l,0,2)if(fr[i][j][l]){
    			REP(j2,0,2)if(!j||!j2)REP(l2,0,2)if(bk[i+1][j2][l2]){
    				(dp2[p][j|j2][l|l2]+=fr[i][j][l]*bk[i+1][j2][l2])%=MOD;
    			}
    		}
    		dfs4(p,x);
    	}
    }
    void Main() {
    	cin>>n>>d;
    	REP(i,1,n){
    		int x,y;
    		cin>>x>>y;
    		--x,--y;
    		v[x].pb(y);v[y].pb(x);
    	}
    	dfs(0,-1);g[0]=1;dfs2(0,-1);
    	int cnt0=0,cnt1=0;
    	REP(i,0,n)if(f[i]||!g[i])++cnt1;else ++cnt0;
    	dfs3(0,-1);dfs4(0,-1);
    	M.c=dp[0][1][1];M.d=dp[0][1][0];
    //	cout<<'!'<<dp[1][1][1]<<' '<<dp2[1][1][1]<<' '<<M.c<<' '<<M.d<<endl;
    	matrix M2=M;
    //	cout<<dp2[1][0][0]<<' '<<dp2[1][1][1]<<endl;
    	REP(i,1,n){
    //		cout<<M.c<<' '<<M.d<<endl;
    		REP(j,0,2)REP(l,0,2)if(dp[i][j][l]){
    			REP(l2,0,2)if(dp2[i][1-j][l2]){
    //				cout<<j<<' '<<l<<' '<<1-j<<' '<<l2<<' '<<dp[i][j][l]<<' '<<dp2[i][1-j][l2]<<endl;
    				if(l||!l2)(M.c+=dp[i][j][l]*dp2[i][1-j][l2])%=MOD;
    				else (M.d+=dp[i][j][l]*dp2[i][1-j][l2])%=MOD;
    			}
    		}
    	}
    	M.a=n*cnt1%MOD;M.b=n*cnt0%MOD;
    	M2.a=n*(f[0]||!g[0]);M2.b=n*(f[0]||!g[0]);
    	M=M.qpow(d-1)*M2;
    	cout<<(M.a*cnt1+M.c*cnt0)%MOD<<endl;
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("tree.in","r",stdin);
    	freopen("tree.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    
    • 1

    Information

    ID
    3074
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By