HDU - 6446 Tree and Permutation
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6446
本题是一个树上的问题——DFS。
一棵N个结点的树,其结点为1~N。树具有N-1条边,每一条边具有一个权值。
1~N具有N!个不同的排列,第i(1≤i≤N!)个排列记为P[i],第i个排列中的第j(1≤j≤N)个数记为P[i][j]。
对于第i个排列P[i],在树上沿最短路依次通过P[i][1]~P[i][N]。记最短路的权值和为S[i],求解:
$\sum_{i=1}^{N!} S_i \mod M$
考虑1~N间的两个不同的数u、v。在N!个排列中,v恰好为u的后继的排列数为(N-1)!。于是,若记u→v的最短路为D(u,v),则所求答案为:
$(N-1)!\:*\sum_{u\ne v}D(u,v)\mod M$
现在,考虑式:
$f(T)=\sum_{u<v}D(u,v)\cdots(*)$
接下来,考虑树的结点与边。无向树上的每一个结点都是树的割顶,每一条边都是树的桥。于是,树上的每一条边将树划分为两棵子树。考虑连接结点u、v的边:这条边将树划分为以结点u为根的子树、和以结点v为根的子树。设以结点u为根的子树的结点数为cnt[u],则以结点v为根的子树的结点数为cnt[v]=N-cnt[u]。
回到式(*)中,则边(u,v)对式子的贡献为$w(u,v)*cnt[u]*cnt[v]$。
考虑通过DFS预处理cnt[]:选取某一个结点作为根结点,通过DFS预处理以结点u为根的子树的结点数cnt[u]。之后,遍历树上的边:连接结点v与其父结点的边,其对式子的贡献为$w(p[v],v)*cnt[v]*(N-cnt[v])$。
考虑到双向,答案为$ans=2(N-1)!\:*f(T)\mod M$。
参考程序如下:
#include <bits/stdc++.h>
using namespace std; #define MAX_N 100005 const int64_t mod = 1e9 + ; struct arrow
{
int to;
int cost;
arrow(int to = , int cost = ) : to(to), cost(cost) {}
}; int64_t fact[MAX_N]; void init(void)
{
fact[] = ;
for (int i = ; i < MAX_N; i++) fact[i] = fact[i - ] * i % mod;
} int n;
vector<arrow> adj[MAX_N]; int64_t ans;
int cnt[MAX_N]; void dfs_cnt(int u, int p)
{
cnt[u] = ;
for (int i = ; i < adj[u].size(); i++) {
int v = adj[u][i].to;
if (v == p) continue;
dfs_cnt(v, u);
cnt[u] += cnt[v];
}
} void dfs_calc(int u, int p)
{
for (int i = ; i < adj[u].size(); i++) {
int v = adj[u][i].to;
int w = adj[u][i].cost;
if (v == p) continue;
int64_t cur = 1ll * cnt[v] * (n - cnt[v]);
ans += cur * w % mod;
ans %= mod;
dfs_calc(v, u);
}
} int main(void)
{
ios::sync_with_stdio(false);
init();
while (cin >> n) {
memset(cnt, , sizeof(cnt));
for (int i = ; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(arrow(v, w));
adj[v].push_back(arrow(u, w));
}
ans = ;
dfs_cnt(, -);
dfs_calc(, -);
cout << 2ll * ans % mod * fact[n - ] % mod << endl;
for (int i = ; i <= n; i++) adj[i].clear();
}
return ;
}
HDU - 6446 Tree and Permutation的更多相关文章
- (1009) HDU 6446 Tree and Permutation(规律+树上各个点的距离和)
题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和. 分析: 经过简单的分析可以得知,全部的贡献其实相当与(这 ...
- HDU 6446 Tree and Permutation(赛后补题)
>>传送门<< 分析:这个题是结束之后和老师他们讨论出来的,很神奇:刚写的时候一直没有注意到这个是一个树这个条件:和老师讨论出来的思路是,任意两个结点出现的次数是(n-1)!, ...
- Tree and Permutation (HDU 6446) 题解
// 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5868 Different Circle Permutation(burnside 引理)
HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...
- hdu 5225 Tom and permutation(回溯)
题目链接:hdu 5225 Tom and permutation #include <cstdio> #include <cstring> #include <algo ...
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
- HDU6446 Tree and Permutation(树上DP)
传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- springboot根据yml配置文件选择性加载bean
@Slf4j @Aspect @Component @ConditionalOnProperty(value = "localCache.apiCache", havingValu ...
- 多条件查询测试用例设计方法(1)—Pairwise(转)
在我的工作中,我也遇到类似需求.正交法是一种不错的选择,而在我们实践过程中,我们还用了Pairwise方法,以及另一种方法(如下): 假设查询因子:A,B,C,D,E 1.单独查询:A:B:C:D:E ...
- bzoj 1050: [HAOI2006]旅行comf【枚举+并查集】
m是5000,就想到了直接枚举比例 具体做法是是先把边按照边权从小到大排序,然后先枚举最小边权,再枚举最大边权,就是从最小边权里一个一个加进并查集里,每次查st是否联通,联通则退出,更新答案 #inc ...
- bzoj 4297: [PA2015]Rozstaw szyn【瞎搞】
从叶子往上先拓扑一下,建立虚拟root,从root开始dfs.注意到每个点的最优取值一定是一个区间(中位数区间),从儿子区间推出父亲区间即可 #include<iostream> #inc ...
- Unity项目 - Boids集群模拟算法
1987年Craig W.Reynolds发表一篇名为<鸟群.牧群.鱼群:分布式行为模式>的论文,描述了一种非常简单的.以面向对象思维模拟群体类行为的方法,称之为 Boids ,Boids ...
- svg image 图片无法铺满 circle 的问题解决
引子 使用d3.js绘制了力布图后,需要在circle中绘制图片,方法如下: // 绘制图片 drawPattern(gContainer) { let that = this; let gPatte ...
- layui 动态左树导航栏显示样式BUG规避
先看问题现象: 使用 layui 的左树功能,先在html页面添加左树功能引入 <ul class="layui-nav layui-nav-tree layui-nav-side&q ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- C#结构体+结构体与类的区别
C# 结构(Struct) 在 C# 中,结构是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构. C# 结构的特点 您已经用了一个简单的名为 Boo ...
- 数据传递-------@ResponseBody
1.导入jar包 jack-core-asl-1.9.11.jar jack-mapper-asl-1.9.11.jar 2.配置springmvc-servlet.xml文件 <?xml ve ...