题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6446

题目给出的数据为一棵树,dfs扫描每条边,假设去掉某条边,则左边 x 个点,右边 n-x 个点,则经过该条边共有 x*(n-x) 种组合,又因为 1~n 全排列有 n! 种,故 a~b,包含 b~a 这条边的贡献为 (n-x)*x*2*(n-1)!*w;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. #define LL long long
  7. const int MOD = 1e9+;
  8. const int N = 1e5+;
  9. struct EDGE {
  10. LL to, length;
  11. };
  12. vector<EDGE> edge[N];
  13. LL n, factorial[N], lchild[N], w[N];
  14.  
  15. int dfs(int k, int pre) {
  16. int size = edge[k].size(), ans=;
  17. for(int i=; i<size; i++) {
  18. if(edge[k][i].to == pre)
  19. w[k] = edge[k][i].length;
  20. else
  21. ans += dfs(edge[k][i].to, k);
  22. }
  23. return lchild[k] = ans;
  24. }
  25.  
  26. int main()
  27. {
  28. factorial[] = ;
  29. for(int i=; i<N; i++)
  30. factorial[i] = factorial[i-]*i%MOD;
  31.  
  32. while(~scanf("%d",&n))
  33. {
  34. LL ans=;
  35. for(int i=; i<=n; i++) {
  36. edge[i].clear();
  37. }
  38. for(int x,y,v,i=; i<n; i++) {
  39. scanf("%d%d%d", &x, &y, &v);
  40. edge[x].push_back({y,v});
  41. edge[y].push_back({x,v});
  42. }
  43.  
  44. dfs(, -);
  45. for(int i=; i<=n; i++) {
  46. LL tmp=;
  47. tmp = lchild[i]*(n-lchild[i])%MOD;
  48. tmp = *factorial[n-]%MOD*w[i]%MOD*tmp%MOD;
  49. ans = (ans+tmp)%MOD;
  50. }
  51. cout<<ans<<endl;
  52. }
  53. return ;
  54. }

【2018 CCPC网络赛】1009 - 树的更多相关文章

  1. 2018 CCPC网络赛

    2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...

  2. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  3. HDU 6444 Neko's loop ( 2018 CCPC 网络赛 && 裴蜀定理 && 线段树 )

    题目链接 题意 : 给出一个 n 个元素的环.可以任意选择起点.选完起点后.可以行走 m 步.每次前进 k 个单位.所走到的点将产生正或负贡献.问你一开始得准备多少才能使得初始资金加上在环上获取最大利 ...

  4. 2018 CCPC 网络赛

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

  5. 2018 CCPC网络赛 几道数学题

    1002 Congruence equation 题目链接  : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zyb ...

  6. 2018 CCPC网络赛 hdu6444 Neko's loop

    题目描述: Neko has a loop of size n.The loop has a happy value ai on the i−th(0≤i≤n−1) grid. Neko likes ...

  7. 2018 CCPC 网络赛 Buy and Resell

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

  8. 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)

    Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...

  9. 【2018 CCPC网络赛】1001 - 优先队列&贪心

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6438 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...

随机推荐

  1. Java--------------Mysql中时间按要求查询

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...

  2. hdu1875 畅通工程再续 暴力+基础最小生成树

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ; ; ; in ...

  3. python之处理json

    import json# json串就是字符串dic={ 'car':{'color':'red','price':100,'count':50}, 'iphone':{'color':'骚粉色',' ...

  4. [洛谷4329/COCI2006-2007#1] Bond

    Description Everyone knows of the secret agent double-oh-seven, the popular Bond (James Bond). A les ...

  5. shell getopts

    1, 分类: LINUX getopts命令内置于shell中,可以获取由单个字符所指定的有效命令行参数,单个字符有一个‘ - ’号或‘ + ’号. 简单的说,比如运行命令: iptables -t ...

  6. [在读]functional javascript

    讲javascript函数化编程的一本书,逛淘宝正好看到有一家卖英文书的,顺手就买了,目前搁置.

  7. CF778B(round 402 div.2 E) Bitwise Formula

    题意: Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied t ...

  8. vmware桥接模式下主机有多个网卡导致虚拟机网络不通

    桥接模式下,vmware会绑定一个物理网卡,因此有多个物理网卡时就要注意当前绑定的物理网卡.打开如下vmware菜单 可以看到VMnet0是桥接模式用的,然后他可以选择绑定一个物理网卡,注意要正确选择 ...

  9. android studio 新建文件出错

  10. FragmentTabHost实现标签卡效果

    转载请注明原文链接:http://www.cnblogs.com/yanyojun/p/8099523.html 代码已上传到github:https://github.com/YanYoJun/Fr ...