Codeforces Round #364 (Div. 2) E DFS



链接:戳这里


E. Connecting Universities
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.



题意:

给出一棵树,然后给出2*k对节点,这2*k个节点上都存在一个价值。

要求将2*k个价值分成k对,使得所有pair之间距离之和最大。输出最大值!


思路:

root为1的树,算k对点对距离和最大,树上不存在最短路一说,两点之间的距离是定死的。

那么怎么组呢?不需要考虑这个,树上的每条边肯定是一些点对的必经之路。

我们只需要枚举一条边的上下两个节点,上部分有多少价值点,下部分有多少价值点,取min。

算作每条边的贡献


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
/// 算每条边的贡献
struct edge{
    int v,next;
}e[400100];
int n,k;
int head[200100],tot=0,vis[200100];
int sz[200100];
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
void DFS(int u,int fa){
    sz[u]=vis[u];
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        DFS(v,u);
        sz[u]+=sz[v];
    }
}
ll ans=0;
void DFS2(int u,int fa){
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        DFS2(v,u);
        ans+=min(sz[v],2*k-sz[v]);
    }
}
int main(){
    mst(head,-1);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=2*k;i++){
        int x;
        scanf("%d",&x);
        vis[x]=1;
    }
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    DFS(1,0);
    ///for(int i=1;i<=n;i++) cout<<sz[i]<<" ";cout<<endl;
    DFS2(1,0);
    printf("%I64d\n",ans);
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值