传送门:点我

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. 同一台电脑中同时安装oracle database 服务器端和oracle client 客户端时注意

    如果在一台电脑中同时安装oracle的客户端和服务器端软件, 一定要先安装oracle database 服务端,并进行相应的配置 listener.ORA. 然后再去安装oracle client ...

  2. freebsd配置ip 网关 子网掩码 DNS

    1.设置IP地址.网关ee  /etc/rc.conf   #编辑ifconfig_em0="inet 192.168.1.173  netmask 255.255.255.0" ...

  3. 关于PostmanURL中不能传递中文的问题

    众所周知,中文乱码等问题在开发过程中让人厌烦,本人最近在使用Postman插件测试web的时候,在请求中添加了中文,结果请求错误 截图如下: 原因:不能直接处理中文,需要自行转化 解决: 1.新建一个 ...

  4. autotools

    文章目录 原文地址 Autotools上手指南1--autoconf基本思想 Autotools上手指南2--autoscan生成configure.ac Autotools上手指南3--autohe ...

  5. jsp案例--展示数据库中的数据

    一.什么是jsp? JAVA SERVER PAGES java的动态网页,servlet用来获取数据处理业务,擅长处理与java代码有关的内容.jsp展示数据,擅长处理与html有关的内容. 二.如 ...

  6. 监控端口是否开放,端口未开放关闭虚拟ip,端口开放启动虚拟IP

    #!/bin/bash#该脚本监控本机的一个端口,当端口异常时,停止lvs的本地ip直到恢复.该脚本依托于lvs.sh启动脚本#目前只支持监控1个vip #定义常用变量#配置检查的ip以及端口chec ...

  7. shell(3)拼写检查与词典操作

    1:Linux下,在/usr/share/dict下包含了词典文件,检查一个单词是否在词典里: #!/bin/bash #文件名:checkout.sh #检查给定的单词是否为词典中的单词 word= ...

  8. postgresql jdbc 连接数据库测试

    转载自:http://blog.csdn.net/southflow/article/details/5944107 1. 下载postgresql-8.4-702.jdbc4.jar  2. 点击 ...

  9. sql server 作业收缩数据库

    USE[master] GO ALTER DATABASE PayFlow2 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE PayFlow2 S ...

  10. jenkins构建触发器详解-不登录触发远程构建详解

    利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...