Codeforces 700B Connecting Universities - 贪心
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 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
6
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 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.

题目大意 给定一个边权都为1的无向连通图,和2k个点,将这2k个点两两进行配对,将每对的距离求和,问最大的距离和是多少?
首先看在最优的配对方案有没有什么规律,然而发现并没有。
既然不能快速地搞定最优配对方案,那可以考虑每个点连向父节点的边。
用f[i][j]表示第i个点,在第i个点的子树内有j个点还没有完成配对对答案的贡献。
转移是什么?+j。这个诡异的转移肯定有问题。
由于+j转移对后面的状态没有什么限制,所以开始贪心。。
显然在i的子树内没有完成配对的点数越多越好,当然要合法,所以就将i子树内被钦定的点数和剩余的被钦定的点数取最小值,然后直接加给答案。
Code
/**
* Codeforces
* Problem#400B
* Accepted
* Time: 62ms
* Memory: 13480k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
#define ll long long
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
const signed int inf = (signed) (~0u >> );
const signed ll llf = (signed ll) (~0ull >> ); template<typename T>
inline void readInteger(T& u) {
static char x;
while(!isdigit(x = getchar()));
for(u = x - ''; isdigit(x = getchar()); u = u * + x - '');
} typedef class Edge {
public:
int end;
Edge* next; Edge(int end = , Edge* next = NULL):end(end), next(next) { }
}Edge; typedef class MapManager {
public:
int ce;
Edge **h;
Edge *edge; MapManager() { }
MapManager(int n, int m):ce() {
h = new Edge*[(n + )];
edge = new Edge[(m + )];
memset(h, , sizeof(Edge*) * (n + ));
} void addEdge(int u, int v) {
edge[ce] = Edge(v, h[u]);
h[u] = edge + (ce++);
} void addDoubleEdge(int u, int v) {
addEdge(u, v);
addEdge(v, u);
} Edge* start(int node) {
return h[node];
}
}MapManager; int n, m;
boolean* isspy;
MapManager g; inline void init() {
readInteger(n);
readInteger(m);
m <<= ;
isspy = new boolean[(n + )];
g = MapManager(n, * n);
memset(isspy, false, sizeof(boolean) * (n + ));
for(int i = , x; i <= m; i++) {
readInteger(x);
isspy[x] = true;
}
for(int i = , u, v; i < n; i++) {
readInteger(u);
readInteger(v);
g.addDoubleEdge(u, v);
}
} ll res = ;
int dfs(int node, int fa) {
int rt = isspy[node];
for(Edge* it = g.start(node); it; it = it->next) {
if(it->end == fa) continue;
rt += dfs(it->end, node);
}
res += min(m - rt, rt);
return rt;
} inline void solve() {
dfs(, );
printf(Auto, res);
} int main() {
init();
solve();
return ;
}
Codeforces 700B Connecting Universities - 贪心的更多相关文章
- codeforces 700B Connecting Universities 贪心dfs
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...
- Codeforces 701E Connecting Universities 贪心
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...
- Codeforces 700B Connecting Universities(树形DP)
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...
- CodeForces 700B Connecting Universities
统计每一条边的贡献,假设$u$是$v$的父节点,$(u,v)$的贡献为:$v$下面大学个数$f[v]$与$2*k-f[v]$的较小值. #pragma comment(linker, "/S ...
- 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 ...
- 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 ...
- codeforces 701E E. Connecting Universities(树的重心)
题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- Connecting Universities
Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...
随机推荐
- cocos2d JS-(JavaScript) cc.each循环遍历对象
有了它,妈妈再也不用担心我的数组会越界啦!! each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSO ...
- Windows jmeter配置
JMeter是Apache软件基金会的产品,用于对提供静态的和动态的资源服务器性能的测试.是一款很方便的测试软件. JMeter 要依附Java SE 环境 所以在启用JMeter之前要安装JAVA ...
- OEMCC 13.2 安装部署
需求:安装部署OEM 13.2 环境:两台主机,系统RHEL 6.5,分别部署OMS和OMR: OMS,也就是OEMCC的服务端 IP:192.168.1.88 内存:12G+ 硬盘:100G+ OM ...
- linux工作目录切换命令
1.pwd命令 pwd命令用于显示用户当前所处的工作目录,格式为“pwd [选项]”. 2.cd命令 cd命令用于切换工作路径,格式为“cd [目录名称]”. 这个命令应该是最常用的一个Linux命令 ...
- Rpgmakermv(38)MOG_Theatrhythm
1.============================葡萄牙语=================================================+++ MOG - Theatrh ...
- skynet 报错 skynet 服务缺陷 Lua死循环
我的报错如下: 看起来是skynet中lua死循环,实际上,可能只是本地配置出了问题,比如,我的数据库连接不上了,因为我把别人的配置更新到我本地了,吗,mysql秘密不对 解决办法就是将配置文件中的, ...
- 电脑已连接wifi的密码查询
有时候,想登陆自己家的无线网络(尤其朋友来家里突然要连接无线网络),脑子刹那间一片空白想不起来密码,怎么办呢? 其实,我们可以通过电脑来查看网络的密码,现在分享如何在笔记本电脑上查看连接过的无线网络密 ...
- Python 构造一个可接受任意数量参数的函数
为了能让一个函数接受任意数量的位置参数,可以使用一个* 参数 在这个例子中,rest 是由所有其他位置参数组成的元组.然后我们在代码中把它当成了一个序列来进行后续的计算
- IDEA相关知识整理
一.离线下载插件[也可以通过代理的方式下载插件] http://plugins.jetbrains.com/ 下载离线插件 settings -> plugins -> install p ...
- PHP HTML混写,PHP中把大块HTML文本直接赋值给字符串变量的方法
PHP HTML混写,PHP中把大块HTML文本直接赋值给字符串变量的方法 使用HEREDOC/NOWDOCHEREDOC和NOWDOC是PHP5.3开始支持的一种新特性,它允许在程序中使用一种自定义 ...