题目链接:

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 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
 
 
题意:
 
给n个点的树,现在要求把这2*k个点分成k对,使每对点之间的距离和最大;
 
思路:相当于找这2*k个点形成的树的重心,然后再计算这些点到重心之间的距离和;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+10;
const int maxn=500+10;
const double eps=1e-14; int vis[N],a[N],head[N],cnt,ans,siz,son[N],n,k;
LL ansdis=0; struct Edge
{
int from,to,next,val;
}edge[2*N];
inline void add_edge(int s,int e)
{
edge[cnt].from=s;
edge[cnt].to=e;
edge[cnt].next=head[s];
head[s]=cnt++;
} void dfs(int cur,int fa)
{
son[cur]=vis[cur];
int temp=0;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int fr=edge[i].to;
if(fr==fa)continue;
dfs(fr,cur);
son[cur]+=son[fr];
temp=max(temp,son[fr]);
}
temp=max(temp,2*k-son[cur]);
if(temp<siz||temp==siz&&cur<ans)
{
siz=temp;
ans=cur;
}
return ;
}
void dfs1(int cur,int fa,LL dis)
{
if(vis[cur])ansdis=ansdis+dis;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int fr=edge[i].to;
if(fr==fa)continue;
dfs1(fr,cur,dis+1);
}
return ;
}
inline void Init()
{
mst(head,-1);
cnt=0;
siz=inf;
}
int main()
{
read(n);read(k);
Init();
For(i,1,2*k)read(a[i]),vis[a[i]]=1;
For(i,1,n-1)
{
int u,v;
read(u);read(v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,0);
dfs1(ans,0,0);
cout<<ansdis<<endl;
return 0;
}

  

 

codeforces 701E E. Connecting Universities(树的重心)的更多相关文章

  1. codeforces 685B Kay and Snowflake 树的重心

    分析:就是找到以每个节点为根节点的树的重心 树的重心可以看这三篇文章: 1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63 ...

  2. 【CodeForces】708 C. Centroids 树的重心

    [题目]C. Centroids [题意]给定一棵树,求每个点能否通过 [ 移动一条边使之仍为树 ] 这一操作成为树的重心.n<=4*10^5. [算法]树的重心 [题解]若树存在双重心,则对于 ...

  3. codeforces 701 E. Connecting Universities(树+ 边的贡献)

    题目链接:http://codeforces.com/contest/701/problem/E 题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行 ...

  4. Codeforces Gym 100814C Connecting Graph 树剖并查集/LCA并查集

    初始的时候有一个只有n个点的图(n <= 1e5), 现在进行m( m <= 1e5 )次操作 每次操作要么添加一条无向边, 要么询问之前结点u和v最早在哪一次操作的时候连通了 /* * ...

  5. Codeforces 701E Connecting Universities 贪心

    链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...

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

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

  7. Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序

    题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...

  8. CodeForces - 686D 【树的重心】

    传送门:http://codeforces.com/problemset/problem/686/D 题意:给你n个节点,其中1为根, 第二行给你2~n的节点的父亲节点编号. 然后是q个询问,求询问的 ...

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

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

随机推荐

  1. Mac环境下安装运行splash

    http://blog.csdn.net/chenhy8208/article/details/69391097 最近需要使用scrapy爬虫做一些开发,用到了splash.我本机是mac环境,跳着看 ...

  2. Wish3D用户必看!模型加载失败原因汇总

    上传到Wish3D的模型加载不出来,作品显示页面漆黑一片,是什么原因? 很有可能是操作过程中的小失误,不妨从以下几点检查.还是不行的请加QQ群(Wish3D交流群3):635725654,@Wish3 ...

  3. Spring源代码由浅入深系列三 refresh

    Spring中的refresh是一个相当重要的方法. 它完毕IOC的第一个阶段,将xml中的bean转化为beanDefinition.具体说明如上图所看到的. 在上图中,创建obtainFreshB ...

  4. HDOJ题目2089 不要62(数位DP)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. 苦逼IT才能看懂的笑话

    这是苦逼IT才能看懂的笑话1.栈和队列的区别是啥? 吃多了拉就是队列:吃多了吐就是栈 2.世界上最遥远的距离不是生与死,而是你亲手制造的BUG就在你眼前,你却怎么都找不到她... 3.<c++程 ...

  6. shell脚本编写-自动部署及监控

    1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...

  7. ios文件系统文件目录操作

    对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox). 一个app发布到iPhone上后,目录结构如下: 1.其中获取 app root 可以用 NSHomeDi ...

  8. Struts2拦截器 解决登录问题

    一.了解Struts2 拦截器[Interceptor] 拦截器的工作原理如图  拦截器是由每一个action请求(request)都包装在一系列的拦截器的内部,通过redirectAction再一次 ...

  9. 数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统

    先说一说问题,不知道大家有没有这样的经验,反正我是经常碰到. 举例1,某些网站每隔几天就发邮件给我,每次发的邮件内容都是一些我根本不感兴趣的东西,我不甚其扰,对其深恶痛绝.         举例2,添 ...

  10. CSDN第一期总结之三:Thread的问题(转)

    C#是一门支持多线程的语言,因此线程的使用也是比较常见的.由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(可能.Net不觉得这部分是它所特有的). 那么线程相关的问题 ...