Problem Description
There are N vertices connected by N−1 edges, each edge has its own length.
The set { 1,2,3,…,N } contains a total of N! unique permutations, let’s say the i-th permutation is Pi and Pi,j is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the Pi,1-th vertex to the Pi,2-th vertex by the shortest path, then go to the Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,N-th vertex, let’s define the total distance of this route as D(Pi) , so please calculate the sum of D(Pi) for all N! permutations.
 
Input
There are 10 test cases at most.
The first line of each test case contains one integer N ( 1≤N≤105 ) .
For the next N−1 lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X,Y≤N,1≤L≤109 ) .
 
Output
For each test case, print the answer module 109+7 in one line.
 
Sample Input
3
1 2 1
2 3 1
3
1 2 1
1 3 2
 
Sample Output
16
24
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
 
 
这题看懂题目之后很傻逼的
求全排列中所有1到任一点的距离之和
按边来考虑 就是求贡献了
n个点 n-1条边 所以每条边有(n-1)! 
然后只有当要求的两个点的距离在边的两端的时候才有贡献
由于有从左到右 和从右到左两种方法 
所以最后每条边的贡献为 2L(n-sz[v])*sz[v] ;
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <set>
  7. #include <iostream>
  8. #include <map>
  9. #include <stack>
  10. #include <string>
  11. #include <vector>
  12. #define pi acos(-1.0)
  13. #define eps 1e-6
  14. #define fi first
  15. #define se second
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. #define bug printf("******\n")
  19. #define mem(a,b) memset(a,b,sizeof(a))
  20. #define fuck(x) cout<<"["<<"x="<<x<<"]"<<endl
  21. #define f(a) a*a
  22. #define sf(n) scanf("%d", &n)
  23. #define sff(a,b) scanf("%d %d", &a, &b)
  24. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  25. #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
  26. #define FIN freopen("DATA.txt","r",stdin)
  27. #define gcd(a,b) __gcd(a,b)
  28. #define lowbit(x) x&-x
  29. #pragma comment (linker,"/STACK:102400000,102400000")
  30. using namespace std;
  31. typedef long long LL;
  32. typedef unsigned long long ULL;
  33. const int INF = 0x7fffffff;
  34. const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
  35. const int mod = 1e9 + ;
  36. const int maxn = 1e6 + ;
  37. int n, tot, head[maxn];
  38. LL d[maxn], sz[maxn];
  39. struct node {
  40. int v, nxt;
  41. LL w;
  42. } edge[maxn << ];
  43. void init() {
  44. tot = ;
  45. mem(head, -);
  46. }
  47. void add(int u, int v, LL w) {
  48. edge[tot].v = v;
  49. edge[tot].w = w;
  50. edge[tot].nxt = head[u];
  51. head[u] = tot++;
  52. }
  53. void dfs(int u, int fa) {
  54. sz[u] = ,d[u]=;
  55. for (int i = head[u]; ~i ; i = edge[i].nxt) {
  56. int v = edge[i].v;
  57. if (v == fa) continue;
  58. dfs(v, u);
  59. sz[u] += sz[v];
  60. d[u] = (d[u] + d[v]) % mod;
  61. d[u] = (d[u] + edge[i].w * sz[v] % mod * (n - sz[v]) % mod) % mod;
  62. }
  63. }
  64. int main() {
  65. while(~sf(n)) {
  66. init();
  67. for (int i = ; i < n ; i++) {
  68. int u, v;
  69. LL w;
  70. scanf("%d%d%lld", &u, &v, &w);
  71. add(u, v, w);
  72. add(v, u, w);
  73. }
  74. dfs(, -);
  75. LL f = ;
  76. for (LL i = ; i <= n - ; i++) f = f * i % mod;
  77. LL ans = d[] * f * % mod;
  78. printf("%lld\n", ans);
  79. }
  80. return ;
  81. }
 
 

Tree and Permutation dfs hdu 6446的更多相关文章

  1. Tree and Permutation (HDU 6446) 题解

    // 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...

  2. (1009) HDU 6446 Tree and Permutation(规律+树上各个点的距离和)

    题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和. 分析: 经过简单的分析可以得知,全部的贡献其实相当与(这 ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. HDU6446 Tree and Permutation(树上DP)

    传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  7. Tree and Permutation

    Tree and Permutation 给出一个1,2,3...N的排列,显然全部共有N!种排列,每种排列的数字代表树上的一个结点,设Pi是其中第i种排列的相邻数字表示的结点的距离之和,让我们求su ...

  8. HDU - 6446 Tree and Permutation

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6446 本题是一个树上的问题——DFS. 一棵N个结点的树,其结点为1~N.树具有N-1条边,每一条边具有 ...

  9. HDU 6446 Tree and Permutation(赛后补题)

    >>传送门<< 分析:这个题是结束之后和老师他们讨论出来的,很神奇:刚写的时候一直没有注意到这个是一个树这个条件:和老师讨论出来的思路是,任意两个结点出现的次数是(n-1)!, ...

随机推荐

  1. VMWare Workstation新装CentOS 7无法联网解决办法

    按照这位博主的方法成功解决:http://blog.csdn.net/deniro_li/article/details/54632949

  2. Aizu - 2249

    注意先保证距离最短,再来判断价格 邻接矩阵回朝内存  ,要用邻接表的 #include<bits/stdc++.h> using namespace std; #define inf 0x ...

  3. 深入理解 Vuejs 动画效果

    本文主要归纳在 Vuejs 学习过程中对于 Vuejs 动画效果的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 V ...

  4. [转载]启动tomcat时,一直卡在Deploying web application directory这块的解决方案

    转载:https://www.cnblogs.com/mycifeng/p/6972446.html 本来今天正常往服务器上扔一个tomcat 部署一个项目的, 最后再启动tomcat 的时候 发现项 ...

  5. Internet History,Tecchnology and Security

    Internet History Internet Technologe Internet Secure

  6. iOS- 网络请求的两种常用方式【GET & POST】的区别

    GET和POST 网络请求的两种常用方式的实现[GET & POST] –GET的语义是获取指定URL上的资源 –将数据按照variable=value的形式,添加到action所指向的URL ...

  7. C#中堆和栈的区别?

    http://www.jb51.net/article/55306.htm http://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.h ...

  8. Qt窗口及控件-窗口Close()自动释放

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt-窗口Close()后自动释放空间     本文地址:http://techieliang ...

  9. 【Linux】- Ubuntu 配置mysql远程访问

    ubuntu上安装mysql非常简单只需要几条命令就可以完成. sudo apt-get install mysql-server   安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后 ...

  10. IIS发布 MVC 配置

    E:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll