链接

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. ROS-机器人建模与仿真概论

    前言:无论是因为高昂的设备费用还是为了减少实验次数,仿真都是十分必要的. ROS提供了很多优秀的仿真方式,下面来介绍一下: URDF:Unified Robot Description Format, ...

  2. input[type='file']获取上传文件路径案例

    最近在项目时,需要获取用户的上传文件的路径,便写了一个demo: <body> <input type="file" name="" valu ...

  3. ibatiS启动的异常 The content of elements must consist of well-formed character data or markup

    ibatiS启动的异常 The content of elements must consist of well-formed character data or markup 配置的动态SQL语句里 ...

  4. php语法学习:轻松看懂PHP语言

    基础语法 开头结尾 PHP脚本以 "<?php " 开头以 "?>" 结尾 <!DOCTYPE html> <html>&l ...

  5. Thingworx SDK开发自定义Widget

    Thingworx自带的图表数量有限,样式也很有限,在echarts上看到了这样一个非常简单的图表,下面将做一个简单的静态引入示范 首先创建Thingworx项目 然后右键ui新建widget 自动生 ...

  6. C/C++函数调用约定与this指针

    关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的. VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调 ...

  7. CorelDRAW升级计划--如何购买

    了解通过全新 CorelDRAW 2017升级计划更新此图形设计软件所有最新功能的实惠方案.助升级计划,您可以在下一主要产品版本推出时便收到该版本,从而始终使您的产品保持最新.升级计划取代为 X6 和 ...

  8. 路飞学城Python-Day29(第四模块-并发编程)

    01-进程与程序的概念 并发:多进程和多线程 进程的概念:进程就是正在执行的过程,一个应用程序不是进程,只有应用程序启动以后才能说是进程,进程是一个抽象的概念,起源于操作系统 02-操作系统介绍 应用 ...

  9. python3配置爬虫开发环境

    爬虫:环境搭建 安装python3: 安装python版本:3.7.0 winsdows下的配置:

  10. Linux下MySql数据库常用操作

    1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...