传送门:点我

Tree and Permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2191    Accepted Submission(s): 826

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≤10^9 ) .
 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
 
 
 
  • 题目大意:
         第一行给定n,表示n个顶点,后面n-1行代表边的两个端点和边的权值。
         要输出的是n个顶点全排列对所有情况,按点依次访问的时候,经过的权值和。
         比如说样例1:
         三个顶点,即对1,2,3全排列,共有6种方式。(1->2->3,1->3->2.......)
         按每种情况的访问点的顺序累加权值。
  • 思路:
         这题考虑的是每条边对答案的贡献。
         考虑顶点有n个的情况:
         假设顶点x和顶点y有边相连,那么在全排列中这条边会被访问几次?
  • 固定x,对其余所有顶点进行全排列,得到(n-1)!种情况。
  • 固定y,对其余所有顶点进行全排列,得到(n-1)!种情况。
         显而易见,每条边在全排列中贡献了2*(n-1)!次,再乘上每2个点的最小路径dis[x][y],就是每条边对答案的贡献,即2*(n-1)!*dis[x][y]
         任意两点距离最短路径参考HDU5723  HDU2376
代码:
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
#define pi acos(-1.0)
#define Max_N 1001
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9+;
LL dp[];
int sum[];
int n;
vector<pair<int,LL> >v[];
LL f[];
void dfs(int now,int father){
sum[now] = ;
for(int i = ; i < v[now].size() ; i++){
int son = v[now][i].first;
LL len = v[now][i].second;
if(son == father)continue;
dfs(son,now);
sum[now] += sum[son];
dp[now] += (dp[son]+((n-sum[son])%mod*sum[son])%mod*len%mod*2LL*f[n-]%mod)%mod;
dp[now] = (dp[now]+mod)%mod;
}
}
int main()
{
f[]= ; f[] = ;
for(int i = ; i <= ; i ++){
f[i]=(f[i-]*i)%mod;
}
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
for(int i = ; i <= n ; i++)v[i].clear();
for(int i = ; i < n-; i ++){
int x,y;
LL w;
scanf("%d %d %lld",&x,&y,&w);
v[x].push_back(make_pair(y,w));
v[y].push_back(make_pair(x,w));
}
dfs(,-);
printf("%lld\n",dp[]);
}
}
/*
3
1 2 1
2 3 1
3
1 2 1
1 3 2
*/

HDU6446 Tree and Permutation(树上DP)的更多相关文章

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

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

  2. 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 ...

  3. HDU6446 Tree and Permutation(树、推公式)

    题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和 思路: 对每一条边,边左边有x个点,右边有y个点,x+y= ...

  4. 【HDU6647】Bracket Sequences on Tree(树Hash 树上Dp)

    题目链接 大意 给出一颗树,按下列方式生成一个括号序列. function dfs(int cur, int parent): print('(') for all nxt that cur is a ...

  5. Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)

    题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...

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

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

  7. Tree and Permutation (HDU 6446) 题解

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

  8. 【题解】彩色树 51nod 1868 虚树 树上dp

    Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...

  9. Codeforces 791D Bear and Tree Jump(树形DP)

    题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...

随机推荐

  1. SQL-存储过程-010

    什么是存储过程? 可以理解为数据库中的方法,与C#中的方法一样,具有参数和返回值: 存储过程的优点? 提高运行速度:存储过程在创造是进行编译,以后运行存储过程都不需要再进行编译,极大化的提高了数据库的 ...

  2. Linux LVM使用小记

    对于Linux LVM一直不太理解,直到最近使用了简单功能后才稍微明白点. 对于硬盘空间物理上的使用,我们都是先对硬盘进行分区,然后格式化成文件系统支持的类型,最后给操作系统使用.但是这种使用方式很不 ...

  3. COMBIN14简单应用

    目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...

  4. linux --- 1.初始linux

    一.计算机简单认识 1.服务器的硬件 ①输入单元:键盘,鼠标,读卡器,触摸屏,手写板 ②主机部分:主板,cpu,显卡,内存条,硬盘,网卡,声卡,电池,散热器 ③输出单元:显示器,打印机 2.内存,cp ...

  5. py-day2-5 python 百分号字符串拼接

    #### 字符串格式化. # %s 代替任何的元素 (数字,字符串,列表··) print('I live %s crty' %'my') print('I live %s crty' %'[6,8, ...

  6. Linux whereis命令详解

    Linux whereis命令 Linux whereis命令用于查找文件. 该指令会在特定目录中查找符合条件的文件.这些文件应属于原始代码.二进制文件,或是帮助文件. 该指令只能用于查找二进制文件. ...

  7. centos 7.x开放端口

    1. 查看已打开的端口 # netstat -anp 2. 查看想开的端口是否已开 # firewall-cmd --query-port=666/tcp 若此提示 FirewallD is not ...

  8. C现代编程

    1.C语言没有像面向对象语言一样提供访问控制的功能,无法实现数据隐藏,可以通过规定成员命名来规避这个问题,例如不允许直接访问的成员以“_”开头. 2.模板模式,利用函数指针,抽离固有代码,差异代码放到 ...

  9. 测试技术/网游Bug分析/单机修改 视频教程

    早期做的一些视频,测试技术/Bug讲解/单机修改,有兴趣的同学自行下载看吧 由于是早期录制的,有口误多包涵... 链接: http://pan.baidu.com/s/1i5JUKPf 密码: a1x ...

  10. node升级的正确方法

    本文主要是针对安装了node的用户如何对node进行升级或者安装指定版本:没有安装node的可以参考连接node安装方法 . 安装方法: 1.产看node版本,没安装的请先安装: $  node -v ...