codeforces 701E. Connecting Universities 树

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 0001 ≤ 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个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行连边,使得所有连出来的边的和最大,每条边边权为1.


解法:他们说找根据这2*k个点找树的重心,求每个学校的到重心的距离加起来就是答案。虽然感觉是正确的,但是不能理解为什么是正确的。

然后就去看了下别人的做法,理解了下。意思就是计算每条边在计算过程中被加了多少次。

首先对需要连边的点进行标记,

可以理解的是,对于每个点,一定是找离他最远的一个点进行连边,那么对于每条边,他左边有被标记点,右边也有被标记的点,要连接最远的边,一定是有左边的点去连接右边的点,这样这条边会被经过多少次呢,是左边标记点和右边标记点取最小。假设这条边左边有a个被标记的点,右边自然有2*k-a个点,两边的点进行连接,这条边被经过的次数就是min(a,2*k-a)次。然后统计进答案就行了。


CODE

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
struct node{
    int en,next;
}E[N*2];      ///邻接表存边
int n,k;
int top;      ///邻接表用
LL ans;       ///记录答案
int vis[N];   ///标记哪些点有学校
int head[N];  ///邻接表头结点
int son[N];   ///每个点儿子数量

void add(int u,int v){
    E[top].en = v;
    E[top].next = head[u];
    head[u] = top++;
}

void dfs(int u,int fa){
    if(vis[u]) son[u] = 1;           ///对于标记了的点才计算儿子数量
    for(int i = head[u];i != -1;i = E[i].next){
        int v = E[i].en;
        if(v == fa) continue;
        dfs(v,u);
        son[u] += son[v];
    }
    ans += min(son[u],2*k-son[u]);   ///取小
}

int main(void)
{
    scanf("%d%d",&n,&k);
    memset(head,-1,sizeof head);
    for(int i = 1;i <= 2*k;i++){
        int x;scanf("%d",&x);
        vis[x] = true;
    }
    for(int i = 1;i < n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);add(b,a);
    }
    dfs(1,-1);
    printf("%I64d",ans);
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值