CF700B.Connecting Universities

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

In Treeland there are 2k2k 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 kk 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 11 .

输入格式

The first line of the input contains two integers nn and kk ( 2<=n<=2000002<=n<=200000 , 1<=k<=n/21<=k<=n/2 ) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 11 to nn .

The second line contains 2k2k distinct integers u1,u2,...,u2ku_{1},u_{2},...,u_{2k} ( 1<=ui<=n1<=u_{i}<=n ) — indices of towns in which universities are located.

The next n1n-1 line contains the description of roads. Each line contains the pair of integers xjx_{j} and yjy_{j} ( 1<=xj,yj<=n1<=x_{j},y_{j}<=n ), which means that the jj -th road connects towns xjx_{j} and yjy_{j} . All of them are two-way roads. You can move from any town to any other using only these roads.

输出格式

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

输入输出样例

  • 输入#1

    7 2
    1 5 6 2
    1 3
    3 2
    4 5
    3 7
    4 3
    4 6
    

    输出#1

    6
    
  • 输入#2

    9 3
    3 2 1 6 5 9
    8 9
    3 2
    2 7
    3 4
    7 6
    4 5
    2 1
    2 8
    

    输出#2

    9
    

说明/提示

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

首页