题目传送门

题目描述:给出一颗树,每条边都有权值,然后列出一个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到死。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=100010;
ll mod=1e9+7;
int head[maxn*2];
struct edge{
int v,Next;
ll w;
}a[maxn*2];
int tot;
inline void addv(int u,int v,ll w){
a[++tot].v=v;
a[tot].Next=head[u];
a[tot].w=w;
head[u]=tot;
}
int n,u,v;
ll w,sum,f[maxn];
inline void init(){
tot=0,sum=0;
CLR(head,-1);
for(int i=1;i<=n;i++)f[i]=1;
}
inline ll dfs(int x,int fa){
for(int i=head[x];i!=-1;i=a[i].Next){
int v=a[i].v;
if(v==fa)continue;
f[x]=(f[x]+dfs(v,x))%mod;
sum=(sum+2*a[i].w%mod*f[v]%mod*((ll)n-f[v]))%mod;
} return f[x];
}
int main(){
while(cin>>n){
init();
for(int i=1;i<n;i++){
scanf("%d%d%lld",&u,&v,&w);
addv(u,v,w);
addv(v,u,w);
}
dfs(1,-1);
ll an=1;
for(int i=n-1;i>1;i--){
an=(an*i)%mod;
}
printf("%lld\n",sum*an%mod);
}
}

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


 

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

Sample Output


 

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. AOP基础-JDK动态代理

    动态代理技术就是用来产生一个目标对象的代理对象的,代理对象应与目标对象(被代理的对象)有相同的方法,实现对目标对象访问的拦截,并增强目标对象的一些功能,而不需要目标对象去做任何的更改,使得目标对象有更 ...

  2. 关于android中出现failed to read row 0,column -1错误

    该错误出现的原因是Cursor.getColumnIndex()的参数列名不存在或者错误,这时返回值为-1.出现该错误

  3. ngx-bootstrap使用01 安装ngx-bootstrap和bootstrap及其使用、外部样式引入

    1 版本说明 2 新建一个angular项目 ng new 项目名 --stayle=scss 代码解释:创建一个样式文件格式为SCSS的angular项目 技巧01:由于我angular-cli的版 ...

  4. Tensorflow fetch和feed

    import tensorflow as tf #Fetch input1 = tf.constant(1.0)input2 = tf.constant(3.0)input3 = tf.constan ...

  5. 前端学习笔记2017.6.12 DIV布局网页

    DIV的功能就是把网页划分成逻辑块的. 看下豆瓣东西页面的布局,我们来分析下. 按照先从上到下的原则,把这个页面分成几个块: 首先是最顶端的这个条,这是一个DIV,我们给它起个名字,叫banner 然 ...

  6. Xcode 运行时找不到xib资源文件

    调试运行时候,提示找不到xib(或者其他)资源文件,在工程中确实看的到该资源文件,到具体运行的资源目录([[NSBundlemainBundle] resourcePath]),没有看到该文件,而其他 ...

  7. ZROI2018提高day2t1

    传送门 分析 考场上写了前20分和|a[i]|<=1的情况,但是因为没开long long爆零了.实际考场上差不多想到正解了,至少当时不会凸壳... 我们发现对于ax2+bx的大小关系我们可以将 ...

  8. Luogu 4409 [ZJOI2006]皇帝的烦恼

    BZOJ 1863 lyd口中的夹B递推. 挺妙的解法. 第一个感觉是找到一个最大的相邻的$a_i + a_{i - 1}$就可以了,但是这个想法大概只对了一半,一半的意思是说只有在$n$为偶数的时候 ...

  9. 打印sql语句

    root->trace hibernate->trace ,然后,改配置 全文搜索:show_sql,将所有的show_sql改为true. 这样,就会显示sql语句了.

  10. 通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven'

    配置Spring时出现如题这个错误,下面是xml的内容 <?xml version="1.0" encoding="UTF-8"?> <bea ...