hdu6446 Tree and Permutation【推公式+dfs】

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

 

【分析】

我们考虑一条边在所有排列中总共贡献的次数。对于一条边E,这条边将树分为了两个部分,设左边有M个点,则右边有N-M个点。我们考虑任意一种排列,a1,a2,...,aN   . 这一排列需要N次动作,对于其中一个动作ai-a(i+1)  ,该次动作会经过边E当且仅当这两个点在边E的两侧,所以这两个点有可能的组合是2*M*(N-M)  (2是因为两侧可以互换),那么剩余的N-2个点的组合是 (N-2)! 也就是说有2M(N-M)(N-2)!中情况这一动作会有贡献,那么总共有N-1次动作 在乘以N-1  所以一条边在所有排列的总贡献为2*M*(N-M)*(N-1)!   然后求出N-1条边的贡献加起来了。

需要DFS去求出每条边左边点的个数和右边点的个数。

【代码】

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-6
14 #define  fi first
15 #define  se second
16 #define  lson l,m,rt<<1
17 #define  rson m+1,r,rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  fuck(x)     cout<<"["<<"x="<<x<<"]"<<endl
21 #define  f(a)        a*a
22 #define  sf(n)       scanf("%d", &n)
23 #define  sff(a,b)    scanf("%d %d", &a, &b)
24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
26 #define  FIN         freopen("DATA.txt","r",stdin)
27 #define  gcd(a,b)    __gcd(a,b)
28 #define  lowbit(x)   x&-x
29 #pragma  comment (linker,"/STACK:102400000,102400000")
30 using namespace std;
31 typedef long long  LL;
32 typedef unsigned long long ULL;
33 const int INF = 0x7fffffff;
34 const LL  INFLL = 0x3f3f3f3f3f3f3f3fLL;
35 const int mod = 1e9 + 7;
36 const int maxn = 1e6 + 10;
37 int n, tot, head[maxn];
38 LL d[maxn], sz[maxn];
39 struct node {
40     int v, nxt;
41     LL w;
42 } edge[maxn << 2];
43 void init() {
44     tot = 0;
45     mem(head, -1);
46 }
47 void add(int u, int v, LL w) {
48     edge[tot].v = v;
49     edge[tot].w = w;
50     edge[tot].nxt = head[u];
51     head[u] = tot++;
52 }
53 void dfs(int u, int fa) {
54     sz[u] = 1,d[u]=0;
55     for (int i = head[u]; ~i ; i = edge[i].nxt) {
56         int v = edge[i].v;
57         if (v == fa) continue;
58         dfs(v, u);
59         sz[u] += sz[v];
60         d[u] = (d[u] + d[v]) % mod;
61         d[u] = (d[u] + edge[i].w * sz[v] % mod * (n - sz[v]) % mod) % mod;
62     }
63 }
64 int main() {
65     while(~sf(n)) {
66         init();
67         for (int i = 1 ; i < n ; i++) {
68             int u, v;
69             LL w;
70             scanf("%d%d%lld", &u, &v, &w);
71             add(u, v, w);
72             add(v, u, w);
73         }
74         dfs(1, -1);
75         LL f = 1;
76         for (LL i = 1 ; i <= n - 1 ; i++) f = f * i % mod;
77         LL ans = d[1] * f * 2 % mod;
78         printf("%lld\n", ans);
79     }
80     return 0;
81 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值