codeforces 701E. Connecting Universities 树
来源:互联网 发布:c语言sum函数 编辑:程序博客网 时间:2024/05/03 05:02
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.
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.
Print the maximum possible sum of distances in the division of universities into k pairs.
7 21 5 6 21 33 24 53 74 34 6
6
9 33 2 1 6 5 98 93 22 73 47 64 52 12 8
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.
题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行连边,使得所有连出来的边的和最大,每条边边权为1.
解法:他们说找根据这2*k个点找树的重心,求每个学校的到重心的距离加起来就是答案。虽然感觉是正确的,但是不能理解为什么是正确的。
然后就去看了下别人的做法,理解了下。意思就是计算每条边在计算过程中被加了多少次。
首先对需要连边的点进行标记,
可以理解的是,对于每个点,一定是找离他最远的一个点进行连边,那么对于每条边,他左边有被标记点,右边也有被标记的点,要连接最远的边,一定是有左边的点去连接右边的点,这样这条边会被经过多少次呢,是左边标记点和右边标记点取最小。假设这条边左边有a个被标记的点,右边自然有2*k-a个点,两边的点进行连接,这条边被经过的次数就是min(a,2*k-a)次。然后统计进答案就行了。
CODE
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N = 2e5+10;struct node{ int en,next;}E[N*2]; ///邻接表存边int n,k;int top; ///邻接表用LL ans; ///记录答案int vis[N]; ///标记哪些点有学校int head[N]; ///邻接表头结点int son[N]; ///每个点儿子数量void add(int u,int v){ E[top].en = v; E[top].next = head[u]; head[u] = top++;}void dfs(int u,int fa){ if(vis[u]) son[u] = 1; ///对于标记了的点才计算儿子数量 for(int i = head[u];i != -1;i = E[i].next){ int v = E[i].en; if(v == fa) continue; dfs(v,u); son[u] += son[v]; } ans += min(son[u],2*k-son[u]); ///取小}int main(void){ scanf("%d%d",&n,&k); memset(head,-1,sizeof head); for(int i = 1;i <= 2*k;i++){ int x;scanf("%d",&x); vis[x] = true; } for(int i = 1;i < n;i++){ int a,b; scanf("%d%d",&a,&b); add(a,b);add(b,a); } dfs(1,-1); printf("%I64d",ans); return 0;}
- codeforces 701E. Connecting Universities 树
- Codeforces 701E Connecting Universities【树的重心】
- CF#701 E. Connecting Universities (DFS)
- Codeforces Round #364 (Div. 2) E. Connecting Universities 贪心 + dfs
- Codeforces Round #364 (Div. 2)E.Connecting Universities
- Codeforces Round #364 (Div. 2) E. Connecting Universities【两次dfs】
- Codeforces Round #364 (Div. 2) E Connecting Universities
- codeforces round364 div2E Connecting Universities
- Codeforces 700B Connecting Universities(树的中心)
- Codeforces 700B Connecting Universities(DFS)
- Connecting Universities
- 51nod 1737 配对 && Codeforces 700B Connecting Universities【树的重心】
- Codeforces Round #364 (Div. 1)-树的带权重心(模板)||贪心-Connecting Universities
- AIM Tech Round 3 (Div. 2) E. Connecting Universities 【构造】
- Codeforces Round #364 (Div. 1) B.Connecting Universities
- Codeforces Round #364 (Div. 1) B. Connecting Universities
- Codeforces Round #364 (Div. 1) B Connecting Universities
- Codeforces Round #364 (Div. 1) B. Connecting Universities(图论)
- 白盒测试&黑盒测试&性能测试
- HADOOP的运行模式
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
- server
- 通知
- codeforces 701E. Connecting Universities 树
- EventBus3.0配置及使用
- $.ajax
- HDU 4614 花瓶与花 数据结构+求和+区间赋值+维护空位
- 暑期dp46道(12)--HDOJ 2191 多重背包+二进制优化
- Android动画进阶—使用开源动画库nineoldandroids
- C3P0连接池配置与使用
- Windows下的Memcache安装 linux下的Memcache安装
- ArrayList简介