传送门:点我

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. Unreal Engine 4 C++ UCLASS构造函数易出错分析

    Unreal Engine 4 C UCLASS构造函数易出错分析 GENERATED_BODY GENERATED_UCLASS_BODY 在Unreal Engine 4的任意类中通常会见到两个宏 ...

  2. 点击select下拉框获取option的属性值

    select下拉框作为前端开发者应该是经常使用的,最近在项目中遇到这样的情况,点击下拉框选项,需要获取所点击的option的属性值,当时想很简单啊,给option加一个点击事件不就行了,然后就加了一下 ...

  3. 关于DBX Framewrok 和 FireDac 的一点随笔

    DBX Framework (dbExpress Framework )用了很长的时间, 一直觉得简单好用,但今天需要连MySQL5.7, 发现已经没办法用了,感觉是时候放弃用它来作数据连接了. 以前 ...

  4. 如何在myeclipse中安装spket插件

    在web开发中,经常会遇到自动提示,比如jquery.extjs等,在myeclipse写这些代码时需要自动提示,就需要安装spket插件,具体方法见下面 工具/原料   myeclipse spke ...

  5. mysql 远程备份

    #远程备份./innobackupex --defaults-file=/etc/my.cnf --no-timestamp --user xxx --host 192.168.1.123 \--pa ...

  6. Flask--(登录注册)抽取视图函数

    视图函数抽取: 在info目录下准备视图业务模块包:modules 在modules中添加首页模块包index 在index包的__init__中导入蓝图 在index的__init__创建蓝图 在i ...

  7. [蓝桥杯]PREV-10.历届试题_幸运数

    问题描述 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的“筛法”生成 . 首先从1开始写出自然数1,,,,,,.... 就是第一个幸运数. 我们从2这个数开始.把所有序号能被2整除的项删除,变 ...

  8. # 20175227 2018-2019-2 《Java程序设计》第二周学习总结

    20175227 2018-2019-2 <Java程序设计>第二周学习总结 教材学习内容总结 1. 根据蓝墨云上的学习视频,自学第二.三章知识,并自行编译调试书上程序. 2. 第二章主要 ...

  9. nodejs 函数 :html2js

    var fs = require("fs"); var path = require("path"); function propStringToMap(ss1 ...

  10. MySQL之Haproxy+Keepalived+MySQL高可用均衡负载部署 (网络摘抄)

    来源于:https://blog.csdn.net/weisong530624687/article/details/71536837?utm_source=blogxgwz3 一.安装主从MySQL ...