Codeforces Round #364 (Div. 2) E. Connecting Universities(从一棵树上挑出n*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 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 thej-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 3
4 5 3 7
4 6
output
6
input
      
      
9 3
3 2 1 6 5 9
8 9
3 2
2 1
2 7 3 4
4 5
7 6
2 8
output
9

题意:给你一棵树,然后给你2*k个点,让它们两两匹配,求总距离的最大值

思路:考虑每条边对答案的贡献,每条边对答案的贡献就是他的子树上有标记的点的数目和剩下的点的数目的较小值

#include<bits/stdc++.h>
using namespace std;
const int maxn=201000;
vector<int>G[maxn];
int vis[maxn],cnt[maxn],k;
long long ans;

void dfs(int u,int pre){
    cnt[u]=vis[u];
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==pre)
            continue;
        dfs(v,u);
        ans+=min(cnt[v],2*k-cnt[v]);
        cnt[u]+=cnt[v];
    }
}

int main(){
    int n,x;
    ans=0;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=2*k;i++){
        scanf("%d",&x);
        vis[x]=1;
    }
    int u,v;
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0);
    printf("%lld\n",ans);
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值