Codeforces 700B - Connecting Universities

本文介绍了一种使用树形DP解决特定图论问题的方法,即如何在给定的一棵树上,找到2k个点的配对方式,使得这些配对间的路径长度之和最大。文章通过实例解析了算法思路,并提供了简洁的代码实现。

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

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.

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 200005
using namespace std;

int n,k;
int x,y;
/*
题目大意:给定一颗树和2k个点,
问如果使2k个点均配对能产生最多多少的路径权重和(路径权重为1).

思维题思维题。。。。
看了别人的题解才恍然大悟,自己的图论感觉已经不够用了。。。

刚开始想的超级复杂,不要用点作为对象,
要用边作为答案的贡献特征。
对于一条边,边的两端分别是两个集合,
那么这条边最大的贡献度是多少呢?min(|集合1|,|集合2|)。
那么对每条边进行dp计数即可。
(脑残的我看了题解还犹豫不决。。。没想到只要求出一端的所有点数即可)。

代码很短,树形dp的思想

*/

ll d[maxn],ans=0;
vector<int> sons[maxn];

int dfs(int u,int v)
{
    for(int i=0;i<sons[u].size();i++)
    {
        int tp=sons[u][i];
        if(tp==v) continue;
        dfs(tp,u);
        ans+=min(d[tp],2*k-d[tp]);
        d[u]+=d[tp];
    }
}


int main()
{
    scanf("%d%d",&n,&k);memset(d,0,sizeof(d));
    for(int i=0;i<2*k;i++) scanf("%d",&x),d[x]=1;

    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        sons[x].push_back(y);
        sons[y].push_back(x);
    }
    dfs(1,0);
    printf("%lld\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值