Codeforces700B. Connecting Universities
来源:互联网 发布:2016淘宝双11成交额 编辑:程序博客网 时间:2024/05/12 09:00
Treeland is a country in which there are n towns connected byn - 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 ink 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 to1.
The first line of the input contains two integers n andk (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 from1 to n.
The second line contains 2k distinct integersu1, 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 integersxj andyj (1 ≤ xj, yj ≤ n), which means that the j-th road connects townsxj andyj. 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 intok 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 number1 and 6 (marked in red) and universities number2 and 5 (marked in blue) by using the cable, the total distance will equal6 which will be the maximum sum in this example.
条路长度都是1,每所大学仅能和另一所大学相连,求出相连的路径和的最大值。
思路:首先这是一颗树,所以它比较特别,任意两点间只有唯一的一条道路相通,所以并不
需要知道它们之间具体是怎么连的,只需要知道每条道路被用过多少次,为了使得路径和最大,
那么就应该使连接的路尽量多。可以用dfs的方法遍历这棵树,每当处理完一个非叶子结点fa后,
记录这个结点的儿子结点里共多少个大学suma,和fa结点之外的大学数量sumb相比较,如果
sumb>suma,那么如果让fa和它的儿子结点互相连接的话,最长的道路就是从左经过fa绕到右,
如果让fa和它的儿子们往外连,它们不但要经过fa并且还要走更远,所以这样能使道路更长。反之,
sumb<suma就要把fa外面的结点往fa的和它的儿子结点里连,这样我们可以根据大学数量求出fa到
fa的父亲这一条路走了多少遍,加入结果。当一遍dfs完之后,答案就出来了。
代码很短。
#include <bits/stdc++.h>using namespace std;#define ll long long int#define maxn 200010struct node{ int to, next;}G[maxn*2];int head[maxn*2], has[maxn], n, k, tot;ll res;bool vis[maxn];int dfs(int fa){ //printf("%d\n", fa); vis[fa] = 1; for(int i = head[fa];i!=-1;i=G[i].next){ int son = G[i].to; if(vis[son]) continue; int tmp = dfs(son); has[fa] += tmp; if(2*k-tmp>=tmp) res += tmp; else res += (2*k-tmp); //printf("%d %d +%d\n", fa, son, res); } return has[fa];}void add(int u, int v){ G[tot].to = v; G[tot].next = head[u]; head[u]=tot++;}int main(){ int i, x, u, v; scanf("%d %d", &n, &k); memset(has, 0, sizeof has); memset(head, -1, sizeof head); for(i = 0;i < 2*k;i++){ scanf("%d", &x); has[x] = 1; } tot = 0; for(i = 0;i < n-1;i++){ scanf("%d %d", &u, &v); add(u, v); add(v, u); } memset(vis, 0, sizeof vis); res = 0; dfs(1); printf("%I64d\n", res);}
- Codeforces700B. Connecting Universities
- [codeforces700B]-Connecting Universities-贪心策略
- Connecting Universities
- CF 700B Connecting Universities
- codeforces round364 div2E Connecting Universities
- Codeforces 700B Connecting Universities(DFS)
- codeforces 701E. Connecting Universities 树
- CF#701 E. Connecting Universities (DFS)
- Codeforecs 700D Connecting Universities 构造
- Codeforces Round #364 (Div. 1) B.Connecting Universities
- Codeforces Round #364 (Div. 2) E. Connecting Universities 贪心 + dfs
- Codeforces Round #364 (Div. 2)E.Connecting Universities
- Codeforces Round #364 (Div. 1) B. Connecting Universities
- Codeforces Round #364 (Div. 1) B Connecting Universities
- AIM Tech Round 3 (Div. 2) E. Connecting Universities 【构造】
- Codeforces Round #364 (Div. 2) E. Connecting Universities【两次dfs】
- Codeforces Round #364 (Div. 1) B. Connecting Universities(图论)
- Codeforces 700B Connecting Universities(树的中心)
- 51nod-1478 括号序列的最长合法子段
- A. Meeting of Old Friends
- NYOJ-311-完全背包(DP)
- 操作系统
- JavaScript 浏览器对象模型2
- Codeforces700B. Connecting Universities
- 基于LPC11E68搭建Keil开发环境
- hadoop添加节点
- LeetCode : Majority Element
- 我是一个线程(对理解多线程很有帮助)
- java 解决double运算精度问题(使用BigDecimal )
- java集合数据结构实现
- 使用PhotonServer和Unity建立一个棋牌类游戏实例(三)Unity客户端的注册请求和服务器的注册请求响应
- 大一的一些事