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

博客围绕Tree and Permutation问题展开,该问题涉及N个顶点由N - 1条边连接,需计算所有N!排列的总距离之和。思路是发现每条无向边会出现(n - 1)!次,将问题转化为求点间距离和,通过dfs预处理子树节点数来计算边出现次数,还给出了相关公式和代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tree and Permutation

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


 

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-1)!次,那么问题就变成了求每个点到其他各个点的距离的和。我们可以算出每条边出现的次数。先随意建一棵树,一条边出现的次数=两边顶点数的乘积*2。可以用dfs预处理出树上每个点的子树的节点数。

公式:(n-1)!*\sum (2*s*(n-s)*e[i].w)

(s=min(dp[e[i].u],dp[e[i].v]))

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005
#define inf 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
int n;
ll fac[MAXN];
struct node
{
    int u,v;
    ll w;
}e[MAXN];
vector<int> vec[MAXN];
int dp[MAXN];
bool vis[MAXN];
int dfs(int u)
{
    int s=1;
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(vis[v]) continue;
        vis[v]=1;
        s+=dfs(v);
    }
    return dp[u]=s;
}
int main()
{
    fac[1]=1;
    for(int i=2;i<MAXN;i++)
        fac[i]=(fac[i-1]*i)%mod;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            vec[i].clear();
        for(int i=1;i<n;i++)
        {
            int u,v;
            ll w;
            scanf("%d%d%lld",&u,&v,&w);
            e[i].u=u;
            e[i].v=v;
            e[i].w=w;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        memset(dp,0,sizeof dp);
        memset(vis,0,sizeof vis);
        vis[1]=1;
        dfs(1);
        ll ans=0;
        for(int i=1;i<n;i++)
        {
            int u=e[i].u;
            int v=e[i].v;
            int s=min(dp[u],dp[v]);
            ans=(ans+2LL*s*(n-s)*e[i].w)%mod;
        }
        printf("%lld\n",(ans*fac[n-1])%mod);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值