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
| #define del(a,i) memset(a,i,sizeof(a)) #define ll long long #define inl inline #define il inl void #define it inl int #define ill inl ll #define re register #define ri re int #define rl re ll #define mid ((l+r)>>1) #define lowbit(x) (x&(-x)) #define INF 0x3f3f3f3f using namespace std; template<class >il read( &x){ int f=1;char k=getchar();x=0; for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1; for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0'; x*=f; } template<class >il _print( x){ if(x/10) _print(x/10); putchar(x%10+'0'); } template<class >il print(T x){ if(x<0) putchar('-'),x=-x; _print(x); } ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;} it qpow(int x,int m,int mod){ int res=1,bas=x; while(m){ if(m&1) res=(1ll*res*bas)%mod; bas=(1ll*bas*bas)%mod,m>>=1; } return res; } const int N = 5e3+5,mod = 1e9+7; int n,m,u,v,d,ans,val[N],sz[N],fac[N],ifac[N],c[N],sum[N],pw[N][N],head[N],num_edge; it add(int x,int y){return x+y>=mod?x+y-mod:x+y;} it mul(int x,int y){return 1ll*x*y%mod;} il inc(int &x,int y){x=add(x,y);} it max(int x,int y){return x>y?x:y;} it min(int x,int y){return x<y?x:y;} struct Edge{int next,to,dis;}edge[N<<1]; il add_edge(int u,int v,int dis){ edge[++num_edge]=(Edge){head[u],v,dis},head[u]=num_edge; edge[++num_edge]=(Edge){head[v],u,dis},head[v]=num_edge; } il DFS(int u,int fa){ sz[u]=1; for(ri i=head[u];i;i=edge[i].next){ int v=edge[i].to; if(v==fa) continue; DFS(v,u),sz[u]+=sz[v]; val[v]=edge[i].dis; } } it C(int n,int m){return mul(mul(ifac[m],ifac[n-m]),fac[n]);} int main(){ freopen("class.in","r",stdin); freopen("class.out","w",stdout); read(n),read(m),fac[0]=1; for(ri i=1;i<=m;++i) fac[i]=mul(fac[i-1],i); ifac[m]=qpow(fac[m],mod-2,mod); for(ri i=m-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1); for(ri i=1;i<=n;++i){ pw[i][0]=1; for(ri j=1;j<=2*m;++j) pw[i][j]=mul(pw[i][j-1],i); } for(ri i=1;i<n;++i){ read(u),read(v),read(d); add_edge(u,v,d); 大专栏 test20200115="line"> } DFS(1,0); for(ri i=0;i<=m;++i) c[i]=C(m,i); for(ri i=0;i<=2*m;++i) for(ri j=0;j<=min(i,m);++j) inc(sum[i],mul(c[j],c[i-j])); for(ri i=1;i<=n;++i){ for(ri j=0;j<=2*m;++j){ int res=mul(pw[sz[i]][j],pw[n-sz[i]][2*m-j]); res=mul(res,min(j,2*m-j)),res=mul(res,sum[j]); inc(ans,mul(res,val[i])); } } print(ans); return 0; }
|