码迷,mamicode.com
首页 > 其他好文 > 详细

网络赛 1009

时间:2018-08-25 20:09:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:bit   scan   str   void   tree   +=   max   网络   \n   

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define rep(i,a,b) for(int i=a;i<=b;++i)
 4 #define ms(arr,a) memset(arr,a,sizeof arr)
 5 #define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
 6 typedef long long ll;
 7 const ll F=1e9+7;
 8 const int maxn=1e5+5;
 9 ll fac[maxn];
10 int sz[maxn];
11 struct node
12 {
13     int to,l;
14     node(){}
15     node(int x,int y):to(x),l(y){}
16 };
17 vector<node> tree[maxn];
18 ll sum;
19 int n;
20 
21 void init_fac()
22 {
23     fac[0]=fac[1]=1;
24     rep(i,2,maxn)
25     {
26         fac[i]=fac[i-1]*i%F;
27     }
28 }
29 int dfs(int fa,int i)
30 {
31     //printf("%d->%d\n",fa,i);
32     int num=tree[i].size();
33     sz[i]=0;
34     rep(k,0,num-1)
35     {
36         if(tree[i][k].to==fa)continue;
37         sz[i]+=dfs(i,tree[i][k].to);
38     }
39     sz[i]+=1;
40     return sz[i];
41 }
42 void calc(int fa,int i)
43 {
44     int num=tree[i].size();
45     rep(k,0,num-1)
46     {
47         int tmp=tree[i][k].to;
48         if(fa==tmp)continue;
49         sum=(sum+1LL*tree[i][k].l*sz[tmp]%F*(n-sz[tmp])%F)%F;
50         calc(i,tmp);
51     }
52 }
53 int main()
54 {
55     freopen("Input.txt","r",stdin);
56     //freopen("Output.txt","w",stdout);
57 
58     int u,v,l;
59     init_fac();
60     while(~scanf("%d",&n))
61     {
62         rep(i,1,n)tree[i].clear();
63         rep(i,1,n-1)
64         {
65             scanf("%d%d%d",&u,&v,&l);
66             tree[u].push_back(node(v,l));
67             tree[v].push_back(node(u,l));
68         }
69         sum=0;
70         dfs(0,1);
71         //rep(i,1,n)debug(sz[i]);
72         calc(0,1);
73         //debug(sum);
74         printf("%lld\n",2*sum%F*fac[n-1]%F);
75     }
76     //freopen("CON","w",stdout);
77     //system("start Output.txt");
78 }

 

网络赛 1009

标签:bit   scan   str   void   tree   +=   max   网络   \n   

原文地址:https://www.cnblogs.com/maoruimas/p/9534878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!