HDU6446 Tree and Permutation(树上DP)
传送门:点我
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
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.
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 ) .
- 题目大意:
- 思路:
- 固定x,对其余所有顶点进行全排列,得到(n-1)!种情况。
- 固定y,对其余所有顶点进行全排列,得到(n-1)!种情况。
#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)的更多相关文章
- hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs
题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...
- 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 ...
- HDU6446 Tree and Permutation(树、推公式)
题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和 思路: 对每一条边,边左边有x个点,右边有y个点,x+y= ...
- 【HDU6647】Bracket Sequences on Tree(树Hash 树上Dp)
题目链接 大意 给出一颗树,按下列方式生成一个括号序列. function dfs(int cur, int parent): print('(') for all nxt that cur is a ...
- Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)
题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- Tree and Permutation (HDU 6446) 题解
// 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...
- 【题解】彩色树 51nod 1868 虚树 树上dp
Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...
- Codeforces 791D Bear and Tree Jump(树形DP)
题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...
随机推荐
- [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]
https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...
- 浅谈一下mshta在CVE-2017-11882里的命令构造
Evi1cg同学前不久放出CVE-2017-11882的一个 python利用脚本,地址在https://github.com/Ridter/CVE-2017-11882/,不过其中一个版本里边有一个 ...
- Centos7.3 之mysql5.7二进制安装
#!/bin/bash #注意,该脚本是在centos7.3非生产环境下测试的,其他版本的系统可能不适用,要根据情况修改.需要先下载好mysql二进制包到本地(我一般都是在root家目录下操作,文件也 ...
- 安装包安装npm
在阿里云机器上centos7安装npm可以直接yum安装,然后基于镜像的时候安装不了,直接使用安装包安装,记录一下: 官网下载地址:https://nodejs.org/en/download/ #! ...
- G2 绘制混合图例 demo
G2 绘制混合图例 demo import G2 from '@antv/g2'; import DataSet from '@antv/data-set'; // G2 对数据源格式的要求,仅仅是 ...
- 关于Java流
- 几种不同格式的json解析
原文地址:http://blog.csdn.net/whx405831799/article/details/42171191 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是 ...
- Python 面向对象(三)
继承的实现原理 Python支持多继承 多继承的时候 属性查找的顺序 研究经典类和新式类在属性查找的不同 主要是形成菱形关系才有深度跟广度 广度优先 Python的继承原理 Python3的内置方 ...
- MFC 中CString 格式16进制转int 十进制
代码:CString v_hex ; int v_dec; v_dec = wcstol(v_hex, NULL, 16);
- Letsencrypt SSL免费证书申请(Docker)
最近需要SSL证书,又不想花钱买,正好看到linux基金会去年底上线了新的开源项目,免费推广SSL遂尝试. Let's Encrypt 介绍 Let’s Encrypt is a free, auto ...