Tree and Permutation(排列组合+树)好题!

Tree and Permutation

http://acm.hdu.edu.cn/showproblem.php?pid=6446

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

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,YN,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

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

Recommend

chendu

思路:

Tree and Permutation(排列组合+树)好题!_第1张图片

代码:

#include
using namespace std;
typedef long long ll;
const int maxn = 100000 + 10;
int a[maxn];
const ll mod = 1e9 + 7;
ll fac[maxn];
struct edge
{
    int to,next;
    ll w;
}e[maxn*2];
int head[maxn];
int tot;
int num[maxn];
ll dp[maxn];
ll ans;
void add(int u,int to,int w)
{
    e[++tot].next=head[u];
    e[tot].w=w;
    e[tot].to=to;
    head[u]=tot;
}
void init()
{
    fac[0]=1;
    fac[1]=1;
    for(int i=2;i

 

 

 

你可能感兴趣的:(DFS,数学)