链接

Codeforces 701E Connecting Universities

题意

n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大

思路

贪心,思路挺巧妙的。首先dfs一遍记录每个点的子树中(包括自己)有多少点是这K个中间的,第二遍dfs时对于每一条边,取两端包含较少的值,这样就保证树中间的点不会被取到,留下的就是相隔更远的点了。方法确实想不到啊。

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string> #define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8 using namespace std;
struct edge{
int v, next;
}edges[400005];
int n, k;
int head[200005], tot = 0, vis[200005];
int sz[200005];
void Add(int u, int v){
edges[tot].v = v;
edges[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int fa){
sz[u] = vis[u];
for (int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].v;
if (v == fa) continue;
dfs(v, u);
sz[u] += sz[v];
}
}
LL ans = 0;
void dfs2(int u, int fa){
for (int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].v;
if (v == fa) continue;
dfs2(v, u);
ans += min(sz[v], 2 * k - sz[v]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &k);
for (int i = 1; i <= 2 * k; i++){
int x;
scanf("%d", &x);
vis[x] = 1;
}
for (int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
Add(u, v);
Add(v, u);
}
dfs(1, 0);
dfs2(1, 0);
printf("%I64d\n", ans);
return 0;
}

Codeforces 701E Connecting Universities 贪心的更多相关文章

  1. 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 poss ...

  2. codeforces 700B Connecting Universities 贪心dfs

    分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...

  3. Codeforces 700B Connecting Universities(树形DP)

    [题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...

  4. CodeForces 700B Connecting Universities

    统计每一条边的贡献,假设$u$是$v$的父节点,$(u,v)$的贡献为:$v$下面大学个数$f[v]$与$2*k-f[v]$的较小值. #pragma comment(linker, "/S ...

  5. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  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 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 ...

  8. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  9. Connecting Universities

    Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...

随机推荐

  1. Ubuntu下安装tim/QQ/微信

    一.安装deepin-wine环境: 上https://github.com/wszqkzqk/deepin-wine-ubuntu页面下载zip包(或用git方式克隆),在“下载”目录下原地解压即可 ...

  2. ubuntu软件卸载方法

    一.查看软件包 1.查看已安装的软件包 dpkg --list 2.查看不知道要删除软件的具体名称 dpkg --get-selections | grep <软件相关名称> 二.卸载 1 ...

  3. 双系统下Ubuntu时间不准问题

    运行命令: sudo apt-get install ntpdate sudo ntpdate time.windows.com sudo hwclock --localtime --systohc ...

  4. C#——单元测试

    测试搭建请看:http://www.cnblogs.com/Look_Sun/p/4514732.html 右键不出现Generate Unit Test选项请参考:http://www.jb51.n ...

  5. sql server 启用数据库的 Service Broker

    使用SqlDependency时要开启Service Broker ,那么SqlDependency是什么 https://www.cnblogs.com/zhaoyihao/p/5663258.ht ...

  6. 五年磨一剑:Java 开源博客 Solo 1.0.0 发布了!

    从 Solo 第一个版本发布至今,已经过去 5 年了.今天我们非常自豪地宣布,Solo 1.0.0 正式发布,感谢一直以来关注 B3log 开源的朋友! 目前 B3log 开源有三款产品: GitHu ...

  7. Android WebView访问网站携带登录认证Cookies和动态自定义的cookies

    最近项目几个页面要复用微信程序的网页.但是需要调用微网站登录接口,返回Cookies,webview访问需要的网页的时候携带. 并且还需要几个其他的动态改变的cookie,目的是根据这几个动态自定义c ...

  8. php--方便好用的函数

    在写代码的时候经常会遇见各种各样的问题,感觉需要写函数解决,其实php中已经给出指定函数了.这些函数就需要我们日常去了解记忆. 1.获取指点年份中某月的天数 cal_days_in_month() & ...

  9. Python 函数部分练习题

    函 数 基 础 1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作2.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 3.写函数,判断用户传入的 ...

  10. webstorm狂吃内存的解决方法

    今天使用webstorm,电脑居然卡死了,我的电脑配置: 运行内存16g,1.5T内存的台式, 后来发现,可以通过设置 内存值大小来解决. 具体办法: 找到WebStorm.exe.vmoptions ...