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
|
#include<cstdio> #include<iostream> #include<cstring> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; #define Debug(x) cout<<#x<<':'<<x<<endl #define INF 0x7fffffff typedef long long ll; const ll maxn=2e5+11; const ll mod=1e9+7; struct Edge{ ll to,w,nx; }e[maxn]; ll head[maxn],f[maxn]; ll tmp,cnt,n,ans;
void init(){ memset(head,0,sizeof(head)); tmp=2;cnt=1;ans=0; }
void add(ll u,ll v,ll w){ e[tmp]={v,w};e[tmp].nx=head[u];head[u]=tmp++; } ll dfs(ll u,ll fu){ ll sum=1; for(ll i=head[u];i;i=e[i].nx){ ll v=e[i].to,w=e[i].w; if(v!=fu){ ll a=dfs(v,u); sum=sum+a; ans=(ans+(a*(n-a)%mod)*w%mod)%mod; } } return sum; } void solve(){ dfs(1,0); ans=(ans*f[n-1])*2%mod; printf("%lld\n",ans); }
int main(){ f[0]=1; for(ll i=1;i<maxn;i++){ f[i]=f[i-1]*i%mod; } while(scanf("%lld",&n)==1){ init(); for(ll i=1;i<=n-1;i++){ ll u,v,w; scanf("%lld%lld%lld",&u,&v,&w); add(u,v,w);add(v,u,w); } solve(); } return 0; }
|