zoukankan      html  css  js  c++  java
  • 网络赛 1009

     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
    ",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
    ",2*sum%F*fac[n-1]%F);
    75     }
    76     //freopen("CON","w",stdout);
    77     //system("start Output.txt");
    78 }
  • 相关阅读:
    进程与线程(二) java进程的内存模型
    进程学习(一) 进程的地址空间
    在一个数组中除两个数字只出现1次外,其它数字都出现了2次
    倒水问题
    leecode 树是否是平衡树 java
    Max Sum
    Encoding
    海阔天空-
    Binomial Showdown
    Square
  • 原文地址:https://www.cnblogs.com/maoruimas/p/9534878.html
Copyright © 2011-2022 走看看