Codeforces Round #364 (Div. 2) E. Connecting Universities

时间:2023-01-16 08:23:01
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 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 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 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.

Codeforces Round #364 (Div. 2) E. Connecting Universities

题意:

给你一棵n个节点的树,然后给你2*k的节点,现在让你使者2*k个节点两两配对,这k对节点的路径和最长

题解:

这题可以求这2*k个点的重心,然后每个点到这个重心的路径和就是答案,也可以算每条边的贡献值,每条边的贡献值为边两端配对点最小的个数

我这里用的树的重心来求

 #include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll; const int N=2e5+;
int n,k,a[N],g[N],v[N*],nxt[N*],ed,x,y,son[N],zhong,zmx=<<;
ll ans=; inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} void dfs(int x=,int pre=)
{
son[x]=a[x];
int mx=;
for(int i=g[x];i;i=nxt[i])if(v[i]!=pre)
dfs(v[i],x),son[x]+=son[v[i]],mx=max(mx,son[v[i]]);
mx=max(mx,*k-son[x]+);
if(zmx>mx)zhong=x,zmx=mx;
} void find(int x=zhong,int pre=zhong,int dis=)
{
if(a[x])ans+=dis;
for(int i=g[x];i;i=nxt[i])if(v[i]!=pre)find(v[i],x,dis+);
} int main()
{
scanf("%d%d",&n,&k);
F(i,,k<<)scanf("%d",&x),a[x]=;
F(i,,n-)scanf("%d%d",&x,&y),adg(x,y),adg(y,x);
dfs(),find(),printf("%I64d\n",ans);
return ;
}

Codeforces Round #364 (Div. 2) E. Connecting Universities的更多相关文章

  1. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; E&period; Connecting Universities &lpar;DFS&rpar;

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  2. Codeforces Round &num;364 &lpar;Div&period; 1&rpar;B&period; Connecting Universities

    题目链接:传送门 题目大意:n个点构成一棵树,给定 k*2 点,要分成 k 组,使每组点之间的距离之和最大. 题目思路:因为是求距离之和最大,所以我们可以知道这样一个性质.如果以一条边为界,两边的子树 ...

  3. Codeforces Round &num;364 &lpar;Div&period; 1&rpar; &lpar;差一个后缀自动机&rpar;

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  4. Codeforces Round &num;364 &lpar;Div&period; 1&rpar;&lpar;vp&rpar; 没什么题解就留坑待填

    我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...

  5. Codeforces Round &num;364 &lpar;Div&period; 2&rpar;

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  6. Codeforces Round &num;364 &lpar;Div&period;2&rpar; D&colon;As Fast As Possible(模拟&plus;推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  7. Codeforces Round &num;364 &lpar;Div&period;2&rpar; C&colon;They Are Everywhere(双指针&sol;尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  8. 树形dp Codeforces Round &num;364 &lpar;Div&period; 1&rpar;B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  9. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; B&period; Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. CCS 6新建TMS320F2812工程

    准备材料 CCS6 下载地址:http://www.ti.com/tool/ccstudio F2812的C语言头文件 下载地址:http://www.ti.com/lit/zip/sprc097 安 ...

  2. C&num;运算符

    运算符 1.算数运算符 赋值运算符 等号在C#中并不是表示相等的意思,而是表示赋值,把等号右边的值赋值给 等号左边的变量 由等号连接的表达式,叫做赋值表达式.我们要求等号两边的数据类型必须一 致. 2 ...

  3. java封装性之private

    public class TestDemo{ public static void main(String args[]){ Person perA= new Person(); perA.setNa ...

  4. APMServ 支持&period;htaccess伪静态

    假如你的APMServ安装在X盘APMServ5.2.6目录的话请按以下步骤做. X:\APMServ5.2.6\Apache\conf\httpd.conf  文件找到你所在的虚拟目录修改以下这个地 ...

  5. &lpar;八&rpar;play之yabe项目【身份验证】

    (八)play之yabe项目[身份验证] 博客分类: 框架@play framework   添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...

  6. sql2008备份集中的数据库备份与现有的xxx数据库不同解决方法

    原文链接:http://wncbl.cn/posts/1993c22/ 问题描述 今天在配置一个 ASP 站点时,导入以前的数据库备份文件,提示:sql2008备份集中的数据库备份与现有的xxx数据库 ...

  7. vim 的 tags 模块 与 ctags

    1. 概述 一般来说,在代码中跳转,离不开 ctags. 实际上,vim 中代码跳转是由 vim tags 模块完成的,tags 模块依赖于 tags 文件. ctags(Generate tag f ...

  8. &lbrack;转&rsqb;C语言单引号和双引号的区别

    单引号和双引号在C中的意义完全不同,包围在单引号中的一个字符只是编写整数的另一种方法.这个整数是给定的字符在实现的对照序列中的一个对应的值,即ASCII码值.因此在一个ASCII实现中,‘a’和014 ...

  9. android高效ORM数据库框架greenDao使用

    因为项目中多处用到了数据库,需要对数据库频繁的读写操作,虽然android 自带的SQLiteOpenHelper的.这种方式比较方便易懂,但是在使用过程中需要写很多的sql语句,而且需要及时的关闭和 ...

  10. Vue 爬坑之路(十)—— Vue2&period;5 &plus; Typescript 构建项目

    Typescript 在前端圈已经逐渐普及,Vue 2.5.0 改进了类型声明,使得对 TypeScript 更加友好 不过要想在项目中直接使用 TypeScript  仍然需要对项目进行一些改造 P ...