Codeforces 701E Connecting Universities【树的重心】

224 篇文章 2 订阅

E. Connecting Universities
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.

In Treeland there are 2k universities which are located in different towns.

Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!

To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.

Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.

Input

The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.

The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.

The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.

Output

Print the maximum possible sum of distances in the division of universities into k pairs.

Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note

The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.



题目大意:

给你一颗具有N个节点的树,然后让你给2*K个点组成K对,使得每一对点的距离和最大。


思路:


类似问题:http://blog.csdn.net/mengxiang000000/article/details/53995858

51Nod上的一个题,是找寻所有点配对的最大距离和。


那么我们两次Dfs.

第一次Dfs找寻树的重心,这个题找的重心不是所有点的重心,而是标记配对点的重心。

那么找寻到当前树的重心过程为:

定义一个变量tmp【i】,表示以i作为根节点,其所有子树中最大的那棵树的点的个数(标记点的个数)。

树的重心点=min(tmp【i】)的那个节点;

那么我们定义d【i】表示以i为根节点的子树节点个数(标记点的个数和)。

很显然:d【i】=Σd【v】+vis【i】(如果vis【i】==1表示这个点是标记点,否则vis【i】=0);

也很显然:d【from】=2*k-d【i】;

那么我们求tmp【i】=max(max(d【v】),2*k-d【i】);

接下来我们只需要在Dfs过程中维护一个最小tmp【i】即可。就能很容易找到一棵树的重心啦~。


2、然后我们找到了树的重心之后,ans=Σ每个标记点到重心点的距离。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
    int len,u,d;
}now,nex;
vector<int>mp[300000];
int vis[300000];
int d[250000];
int root,n,m,minn;
__int64 output;
int Dfs(int u,int from)
{
    if(d[u]==0)
    {
        if(vis[u]==1)
        d[u]=1;
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i];
            if(v==from)continue;
            d[u]+=Dfs(v,u);
        }
        int tmpmaxn=0;
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i];
            tmpmaxn=max(d[v],tmpmaxn);
        }
        tmpmaxn=max(m*2-d[u],tmpmaxn);
        if(tmpmaxn<minn||tmpmaxn==minn&&u<root)
        {
            minn=tmpmaxn;
            root=u;
        }
    }
    return d[u];
}
void dfs(int u,int from,int depth)
{
    if(vis[u])output+=depth;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        dfs(v,u,depth+1);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        minn=0x3f3f3f3f;
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=m*2;i++)
        {
            int x;
            scanf("%d",&x);vis[x]=1;
        }
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        output=0;
        Dfs(1,-1);
        dfs(root,-1,0);
        printf("%I64d\n",output);
    }
}
//重心按照vis点的个数。









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值