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
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
 
 
这题看懂题目之后很傻逼的
求全排列中所有1到任一点的距离之和
按边来考虑 就是求贡献了
n个点 n-1条边 所以每条边有(n-1)! 
然后只有当要求的两个点的距离在边的两端的时候才有贡献
由于有从左到右 和从右到左两种方法 
所以最后每条边的贡献为 2L(n-sz[v])*sz[v] ;
 
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<"x="<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
int n, tot, head[maxn];
LL d[maxn], sz[maxn];
struct node {
int v, nxt;
LL w;
} edge[maxn << ];
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, LL w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
sz[u] = ,d[u]=;
for (int i = head[u]; ~i ; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
sz[u] += sz[v];
d[u] = (d[u] + d[v]) % mod;
d[u] = (d[u] + edge[i].w * sz[v] % mod * (n - sz[v]) % mod) % mod;
}
}
int main() {
while(~sf(n)) {
init();
for (int i = ; i < n ; i++) {
int u, v;
LL w;
scanf("%d%d%lld", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
dfs(, -);
LL f = ;
for (LL i = ; i <= n - ; i++) f = f * i % mod;
LL ans = d[] * f * % mod;
printf("%lld\n", ans);
}
return ;
}
 
 

Tree and Permutation dfs hdu 6446的更多相关文章

  1. Tree and Permutation (HDU 6446) 题解

    // 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...

  2. (1009) HDU 6446 Tree and Permutation(规律+树上各个点的距离和)

    题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和. 分析: 经过简单的分析可以得知,全部的贡献其实相当与(这 ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

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

  4. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

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

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

  6. HDU6446 Tree and Permutation(树上DP)

    传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  7. Tree and Permutation

    Tree and Permutation 给出一个1,2,3...N的排列,显然全部共有N!种排列,每种排列的数字代表树上的一个结点,设Pi是其中第i种排列的相邻数字表示的结点的距离之和,让我们求su ...

  8. HDU - 6446 Tree and Permutation

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6446 本题是一个树上的问题——DFS. 一棵N个结点的树,其结点为1~N.树具有N-1条边,每一条边具有 ...

  9. HDU 6446 Tree and Permutation(赛后补题)

    >>传送门<< 分析:这个题是结束之后和老师他们讨论出来的,很神奇:刚写的时候一直没有注意到这个是一个树这个条件:和老师讨论出来的思路是,任意两个结点出现的次数是(n-1)!, ...

随机推荐

  1. 使用JavaScript判断手机是处于横屏还是竖屏

    移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态.从而根据实际需求而执行相应的程序.通过添加监听事件onorientationch ...

  2. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

  3. [译] JavaScript核心指南(JavaScript Core) 【转】

    本文转自:http://remember2015.info/blog/?p=141#scope-chain 零.索引 对象(An Object) 原型链(A Prototype Chain) 构造函数 ...

  4. 2017-2018-2 20172323 『Java程序设计』课程 结对编程练习_四则运算 2

    相关过程截图 关键代码解释 将运算式分开的代码 String[] result = num.split("\\s"); 将输入的num以空格为间隔符号分开,将每一个间隔开的字符存入 ...

  5. node.js安装部署

    node  js  安装部署学习 CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,链接: http ...

  6. 《剑指offer》---丑数

    本文算法使用python3实现 1. 问题1 1.1 题目描述:   把只包含因子2.3和5的数称作丑数(Ugly Number).判断一个数是否是丑数.   时间限制:1s:空间限制:32768K ...

  7. 【Linux】- CentOS安装Mysql 5.7

    CentOS7默认数据库是mariadb,而不是mysql.CentOS7的yum源中默认是没有mysql的.所以不能使用yum install直接安装. 下载mysql的repo源 cd /usr/ ...

  8. Agile.Net 组件式开发平台 - 驱动开发示例

    首先讲一下概念,此驱动非彼驱动.在Agle.Net中我们将组件规划成两种类型,一种是基于业务的窗体组件,一种是提供扩展功能的驱动组件. 打个比方例如一般系统中需要提供身份证读卡功能,然而市面上有很多种 ...

  9. SQL SERVER技术内幕之7 透视与逆透视

    1.透视转换 透视数据(pivoting)是一种把数据从行的状态旋转为列的状态的处理,在这个过程中可能须要对值进行聚合. 每个透视转换将涉及三个逻辑处理阶段,每个阶段都有相关的元素:分组阶段处理相关的 ...

  10. 第27天:js-表单获取焦点和数组声明遍历

    一.表单 1.this指事件的调用者2.input.value 表单更换内容3.innerHTML更换盒子里的内容,文字.标签都能换.4.isNaN("12")如果里面的不是个数字 ...