CF700B.Connecting Universities
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 2_k_ 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.
树国是一个拥有 n 座城镇的国家,这些城镇由 n−1 条双向道路连接,使得任意两座城镇之间均可互相到达。
树国共有 2k 所大学,分别位于互不相同的城镇中。
最近,总统签署了一项法令,要求将这些大学通过高速网络相互连接。教育部按自己的理解执行该法令,认为只需用电缆将每所大学与另一所大学相连即可——即正式地,该法令即视为已落实!
为了使预算总额最大化,教育部决定将大学两两配对,使得所需电缆的总长度最大。换言之,k 对大学之间的总距离应尽可能大。
请帮助教育部求出该最大总距离。当然,每所大学必须且仅能出现在一个配对中。注意:所有道路长度均为 1。
输入格式
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 2_k_ distinct integers _u_1, _u_2, ..., u_2_k (1 ≤ u__i ≤ 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 x__j and y__j (1 ≤ x__j, y__j ≤ n), which means that the j-th road connects towns x__j and y__j. All of them are two-way roads. You can move from any town to any other using only these roads.
输入的第一行包含两个整数 n 和 k(2≤n≤200000,1≤k≤n/2)—— 分别表示树兰国(Treeland)中的城镇数量以及大学配对的数量。假设城镇编号为 1 到 n。
第二行包含 2k 个互不相同的整数 u1,u2,…,u2k(1≤ui≤n)—— 表示设有大学的城镇编号。
接下来的 n−1 行描述了道路。每行包含一对整数 xj 和 yj(1≤xj,yj≤n),表示第 j 条道路连接城镇 xj 和 yj。所有道路均为双向道路。仅通过这些道路,可以从任意一个城镇到达其他任意城镇。
输出格式
Print the maximum possible sum of distances in the division of universities into k pairs.
输出将大学划分为 k 对时可能获得的最大距离总和。
输入输出样例
输入#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 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.

下图展示了第一个测试用例中一种可能的配对方式。若使用电缆连接编号为 1 和 6 的大学(以红色标出)以及编号为 2 和 5 的大学(以蓝色标出),则总距离为 6,此即该示例中的最大总和。

输入解题思路,AI测评打分。不知道怎么写?