HDU 6446 Tree and Permutation 树上DP

Tree and Permutation

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


 

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

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


题目

给出一棵树,现在给出n!全排列所有的访问顺序。

求出所有的路径和。

考虑所有的排列的和。则可以计算树上每两个节点的距离对答案的贡献。

而每两点的距离和就是树上所有路径的和。

树上所有路径的和用树上dp跑一下,就是说考虑每一条边对路径和贡献。也就是说每一条边被计算了几次。

每一条边被计算了son[u] * (n-son[u]) 次。

组合数计算

假设当前n = 3 存在 n! 种全排列

1 2 3     2 1 3    3 1 2

1 3 2     2 3 1    3 2 1 

可以发现每个排列计算了(n-1)条路径的距离 如1 2 3  计算了[1-2] 和 [2-3] 的距离和,一共有n!*(n-1)条路径被计算

而很多是计算重复。可以数一下一共有几种不同的路径。[1-2] [2-3] [1-3]  三种 一般来说就是C(n,2) 种

于是每一条路径被计算了 n!*(n-1) / C(n,2) 次

v = n! * (n-1) / C(n,2)  = (n-1)! * 2

考虑最终答案

ans = [1-2] * v + [1-3] * v + [2-3] * v 

       = ([1-2] + [1-3] + [2-3]) * v

  = 树上所有路径和 *  v 


代码

/*
 * 题意: 给出一棵树,现在给出n!全排列所有的访问顺序。
 *      求出所有的路径和。
 *      考虑所有的排列的和。则可以计算树上每两个节点的距离对答案的贡献。
 *      而每两点的距离和就是树上所有路径的和。
 *      树上所有路径的和用树上dp跑一下,就是说考虑每一条边对路径和贡献。也就是说每一条边被计算了几次。
 *      每一条边被计算了son[u] * (n-son[u]) 次。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> edge;
const int maxn = 1e5+10;
const ll mod = 1e9+7;
vector<edge> ways[maxn];
ll dp[maxn],fac[maxn];
int son[maxn],n;
inline void init() {
    fac[0] = 1;
    for(int i=1;i<maxn;i++) fac[i] = fac[i-1] * i % mod;
}
void dfs(int rt,int fa) {
    son[rt] = 1;
    for(auto v:ways[rt]) {
        int to = v.first;ll val = v.second;
        if(to == fa) continue;
        dfs(to,rt);
        son[rt] += son[to];
        ll tmp = 1LL * son[to] * (n - son[to]) % mod;
        dp[rt] = (dp[rt] + (dp[to] + tmp * val % mod) % mod) % mod;
    }
}
int main()
{
    init();
    while(~scanf("%d",&n)) {
        memset(dp,0,sizeof(dp));
        memset(son,0,sizeof(son));
        for(int i=0;i<maxn;i++) ways[i].clear();
        int u,v;ll val;
        for(int i=1;i<n;i++) {
            scanf("%d%d%lld",&u,&v,&val);
            ways[u].push_back(make_pair(v,val));
            ways[v].push_back(make_pair(u,val));
        }
        dfs(1,0);
        printf("%lld\n",dp[1]*fac[n-1]%mod*2%mod);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值