Tree and Permutation

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

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≤109 ) .
 

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
 
Source

大概题意:

给一棵N个节点的树, 求按N个节点全排列的顺序走一遍这棵树所得的路径贡献和。

解题思路:

对于所有按照全排列走过的路径,打表会发现,每两点之间的距离都出现了 (N-1)!次,所以我们只需要求出 所有两点之间的距离之和就可以解得最后的答案了。

也就是说我们只需要求出 ∑dis(i, j)  最后 ∑dis(i, j)  * (N-1)! 就是答案了。

∑dis(i, j) 的求法:

我们可以根据当前节点 root 和 它的父节点 fa 的边 v 把一棵树分成两部分, 一部分是 以 fa 为根节点的子树,一部分是以 root 为根节点的子树(即除去这课子树是另一部分)。

如果我们已经预先知道了 fa 到 这棵树每个节点的距离和 sum_dis[ fa ], 以及以 root 为根节点的子树的大小t_size[ root ] ,那么 节点 root 到树上其他节点的距离和我们可以通过 sum_dis[ fa ]转化而来;

因为sum_dis[fa] 减去 v 对上面所分成的树的两部分的影响剩下的就是sum_dis[root] 即sum_dis[root] = sum_dis[fa] - (N-t_size[root])*v - t_size[root]*v ;

因此,我们第一步dfs把 1 到各个节点的距离和求出来,第二步通过对 1 的距离和转换 把其他节点的距离和求出来,最后所有距离和求和得出答案。

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define ll long long int
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e5+;
struct date{
int v, next, len;
}node[MAXN<<];
ll sum_dis[MAXN];
int t_size[MAXN];
int head[MAXN], cnt;
int N; void init()
{
memset(head, -, sizeof(head));
cnt = ;
memset(sum_dis, , sizeof(sum_dis));
memset(t_size, , sizeof(t_size));
}
void add(int from, int to, int weight)
{
node[++cnt].next = head[from];
head[from] = cnt;
node[cnt].v = to;
node[cnt].len = weight;
}
void dfs(int root, int fa, ll dis)
{
sum_dis[] = (sum_dis[]%mod+dis%mod)%mod;
t_size[root] = ;
for(int x = head[root]; x != -; x =node[x].next)
{
if(node[x].v != fa)
{
dfs(node[x].v, root, (dis+node[x].len)%mod);
t_size[root]+=t_size[node[x].v];
}
}
}
void trans(int root, int fa)
{
int vv;
for(int i = head[root]; i != -; i= node[i].next)
{
vv = node[i].v;
if(node[i].v != fa){
sum_dis[vv] = (sum_dis[root]%mod+((1ll*(N-*t_size[vv])*node[i].len)%mod)%mod)%mod;
if(sum_dis[vv] < ) sum_dis[vv] += mod;
trans(vv, root);
}
}
}
int main()
{
int f, t, w;
while(~scanf("%d", &N))
{
init();
for(int i = ; i < N; i++)
{
scanf("%d%d%d", &f, &t, &w);
add(f, t, w);
add(t, f, w);
}
dfs(, , );
trans(, );
ll P = , ans = ;
for(int i = ; i < N; i++)
P = 1ll*P*i%mod;
for(int i = ; i <= N; i++)
ans = (ans + sum_dis[i])%mod;
ans = ans*P%mod;
printf("%lld\n", ans);
}
return ;
}

2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路

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

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  4. HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛

    给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法

    http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...

  6. hdu6444 2018中国大学生程序设计竞赛 - 网络选拔赛 1007 Neko's loop

    Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution

    A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟

    Dream Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

随机推荐

  1. switch case 注意事项

    switch case常见的注意事项: 1.case后面常量值的顺序可以任意,一般按顺序编写 2.default顺序也可以编写在switch中的任意位置 当所有case都不满足时则执行default ...

  2. 第十九章:UTC time和local time的互换

    通常我们服务器端的时间使用UTC格式,避免服务器的时区对最终的时间产生影响.而客户端需要根据具体的时区显示local time,本文将介绍如何将服务器的UTC time(基于asp.net web a ...

  3. pat1014. Waiting in Line (30)

    1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  4. html 跨过CSRF验证

    /* CSRF配置 */function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection r ...

  5. 项目搭建系列之四:SpringMVC框架下使用UrlRewrite实现地址重写

    简单记录一下UrlRewrite实现地址重写功能. 1.pom.xml 在pom.xml增加配置UrlRewrite jar <!-- URL Rewrite --> <depend ...

  6. 【学习笔记】Java实用类:枚举(Enum)

    Java API(Java Application Programming Interface)Java应用程序编程接口... 实用类:Java API提供了几个常用包: 1.java.lang:编写 ...

  7. mysql 字符串转换呈毫秒值

    SELECT CEIL((UNIX_TIMESTAMP('2011-05-31 23:59:59') - UNIX_TIMESTAMP('2011-05-31 00:59:59'))/1000/60/ ...

  8. webpack、babel模块、模块化

    一.webpack介绍 webpack这个工具非常强大,解决了前端很繁琐的一些工具流程繁琐的事情.中文官网链接地址:https://www.webpackjs.com/ 1.为什么要使用webpack ...

  9. 【代码笔记】Java深入学习——实现客户端发送文件到服务器的文件传输

    Server.java package com.huaxin.lesson02; import java.io.FileOutputStream; import java.io.InputStream ...

  10. JS基础学习——对象

    JS基础学习--对象 什么是对象 对象object是JS的一种基本数据类型,除此之外还包括的基本数据类型有string.number.boolean.null.undefined.与其他数据类型不同的 ...