题目传送门

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

思路:对于一个n的全排列,会发现 任意x-y的边在这个全排列中出现的次数是一样的,(x-y和y到x是不一样的边)。也就是说我只需要计算出这个次数,然后再乘以所有边的总和(所有x-y和y-x的和)就可以了。

次数就是 ,(边的总数除以边的种类)化简一下就是2*(n-1)!。

而边的和怎么计算呢,考虑树上的某一条边,这条边在边的总和中出现的次数,就是这条边两个节点的子节点个数相乘。所以dfs一遍就可以算出sum。

这道题还要取模,由于我们算次数的公式没有化简,用逆元算,然后逆元的板子有一部分没取模,wa到死。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<string.h>
  6. #include<sstream>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<stack>
  12. #include<bitset>
  13. #define CLR(a,b) memset(a,b,sizeof(a))
  14. using namespace std;
  15. typedef long long ll;
  16. const int maxn=100010;
  17. ll mod=1e9+7;
  18. int head[maxn*2];
  19. struct edge{
  20. int v,Next;
  21. ll w;
  22. }a[maxn*2];
  23. int tot;
  24. inline void addv(int u,int v,ll w){
  25. a[++tot].v=v;
  26. a[tot].Next=head[u];
  27. a[tot].w=w;
  28. head[u]=tot;
  29. }
  30. int n,u,v;
  31. ll w,sum,f[maxn];
  32. inline void init(){
  33. tot=0,sum=0;
  34. CLR(head,-1);
  35. for(int i=1;i<=n;i++)f[i]=1;
  36. }
  37. inline ll dfs(int x,int fa){
  38. for(int i=head[x];i!=-1;i=a[i].Next){
  39. int v=a[i].v;
  40. if(v==fa)continue;
  41. f[x]=(f[x]+dfs(v,x))%mod;
  42. sum=(sum+2*a[i].w%mod*f[v]%mod*((ll)n-f[v]))%mod;
  43. }
  44. return f[x];
  45. }
  46. int main(){
  47. while(cin>>n){
  48. init();
  49. for(int i=1;i<n;i++){
  50. scanf("%d%d%lld",&u,&v,&w);
  51. addv(u,v,w);
  52. addv(v,u,w);
  53. }
  54. dfs(1,-1);
  55. ll an=1;
  56. for(int i=n-1;i>1;i--){
  57. an=(an*i)%mod;
  58. }
  59. printf("%lld\n",sum*an%mod);
  60. }
  61. }

Tree and Permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 281    Accepted Submission(s): 91

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

  1.  
  2.  

3 1 2 1 2 3 1 3 1 2 1 1 3 2

Sample Output

  1.  
  2.  

16 24

hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs的更多相关文章

  1. HDU6446 Tree and Permutation(树上DP)

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

  2. 2018CCPC网络赛

    A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...

  3. 2018CCPC网络赛A(优先队列,思维)

    #include<bits/stdc++.h>using namespace std;priority_queue<pair<int,int>>q;int main ...

  4. Trace 2018徐州icpc网络赛 思维+二分

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) ...

  5. Features Track 2018徐州icpc网络赛 思维

    Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat moveme ...

  6. hdu6446 Tree and Permutation

    没啥好说的,拆一下贡献就完事了.记dis(x,y)为树上x到y的最短路径,设长度为n的排列中有f(n)个里面x和y相邻(不考虑x和y的顺序),那么f(n)=(n-2)! (n-1) 2,显然这个f(n ...

  7. HDU 6205 2017沈阳网络赛 思维题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...

  8. HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)

    正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...

  9. hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造

    题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k&l ...

随机推荐

  1. chromium浏览器开发系列第二篇:如何编译最新chromium

    说一下为什么这么晚才发第二篇,上周和这周department的工作太多了,晚上都是十点半从公司出发,回家以后实在没有多余的精力去摸键盘了.所以请大家包涵! 上期回顾: chromium源码下载: 1. ...

  2. 【271】IDL-ENVI二次开发

    参考:String Processing Routines —— 字符串处理函数 01   STRING 返回字符串. 02   STRCMP 比较字符串,一样返回1,不一样返回0,默认大小写敏感. ...

  3. 使用/dev/dsp的wav文件播放器源码

    转载于:http://blog.csdn.net/dux003/article/details/5459423 #include #include #include #include #include ...

  4. Composite模式 组合模式

    Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...

  5. bzoj 2653 middle(主席树)

    题面:https://vjudge.net/problem/HYSBZ-2653 博客:https://blog.csdn.net/litble/article/details/78984846 这个 ...

  6. ROS Learning-007 beginner_Tutorials ROS节点

    ROS Indigo beginner_Tutorials-06 ROS节点 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...

  7. JavaPersistenceWithMyBatis3笔记-第1章-001

    一.介绍 1.项目结构 2.数据库结构 二.代码 1.Mapper package com.mybatis3.mappers; import java.util.List; import com.my ...

  8. Luogu 4867 Gty的二逼妹子序列

    BZOJ3809,是权限题. 我永远喜欢莫队. 先莫队一下移下左右指针,然后用一个数据结构维护一下区间$[a, b]$中的颜色的值,跟着指针移动一起修改修改,每一次$query$的时候就相当于查询一下 ...

  9. Luogu 2737 [USACO4.1]麦香牛块Beef McNuggets

    NOIP2017 D1T1 的结论,两个数$a, b$所不能表示出的最大的数为$a * b - a - b$. 听了好几遍证明我还是不会 注意到本题中给出的数都非常小,所以最大不能表示出的数$\leq ...

  10. Inheritance with EF Code First: Part 2 – Table per Type (TPT)

    In the previous blog post you saw that there are three different approaches to representing an inher ...