题目链接:http://codeforces.com/contest/701/problem/E

题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行连边,使得所有连出来的边的和最大,每条边边权为1.

题解:这题有一个巧妙的解法,可以记录一下每一条边的贡献(所谓贡献就是有多少对点连线会经过这条边)

也就是记录一下这条边左端点以左的所有需要连的点x和右端点的数目就是2*k-x。然后边的贡献就是两者取

最小。这样dfs一遍所有的边就行了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
const int M = 2e5 + 10;
bool Is[M];
vector<int>vc[M];
ll ans , son[M];
int n , k , x;
void dfs(int pos , int pre) {
int len = vc[pos].size();
if(Is[pos]) son[pos] = 1;
for(int i = 0 ; i < len ; i++) {
int v = vc[pos][i];
if(v != pre) {
dfs(v , pos);
son[pos] += son[v];
}
}
ans += min(son[pos] , 2 * k - son[pos]);
}
int main() {
scanf("%d%d" , &n , &k);
for(int i = 1 ; i <= 2 * k ; i++) {
scanf("%d" , &x);
Is[x] = true;
}
int u , v;
for(int i = 1 ; i < n ; i++) {
scanf("%d%d" , &u , &v);
vc[u].push_back(v);
vc[v].push_back(u);
}
ans = 0;
memset(son , 0 , sizeof(son));
dfs(1 , -1);
printf("%I64d\n" , ans);
return 0;
}

codeforces 701 E. Connecting Universities(树+ 边的贡献)的更多相关文章

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

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

  2. cf 701 E - Connecting Universities

    Descrition 给你一颗\(n\le 2*10^5\)个点的树, 有\(2*k(2k\le n)\)座大学座落在点上 (任二大学不在同一个点) 求一种两两匹配的方案, 使得距离和最大 即\[ma ...

  3. Codeforces Gym 100814C Connecting Graph 树剖并查集/LCA并查集

    初始的时候有一个只有n个点的图(n <= 1e5), 现在进行m( m <= 1e5 )次操作 每次操作要么添加一条无向边, 要么询问之前结点u和v最早在哪一次操作的时候连通了 /* * ...

  4. Codeforces 701E Connecting Universities 贪心

    链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...

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

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

  7. Connecting Universities

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

  8. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  9. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

随机推荐

  1. maven项目引用错误 和项目结构问题

    解决办法: 鼠标右键   maven ---->update prroject Configuration 然后 maven clean  maven install

  2. UR机器人通信--上位机通信(python)

    一.通信socket socket()函数 Python 中,我们用 socket()函数来创建套接字,语法格式如下: socket.socket([family[, type[, proto]]]) ...

  3. Permission 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  4. 在docker中开启新的container

    当你在启动某个容器类型的时候  如果产生了类似的错误: docker: Error: No such image: b27f5dfcfc70c16d7b135889460def6b3f831bcc72 ...

  5. Docker相关地址

    Docker社区版(CE)地址: https://hub.docker.com/search/?type=edition&offering=community Docker文档地址: http ...

  6. 关于dfs的套路

    void dfs(答案, 搜索层数, 其他参数) { if (层数==maxdeep) { 更新答案 return; } (剪枝) for(下一层可能的状态){ 更新全局变量表示的状态的变量 dfs( ...

  7. 如何思考博弈dp

    两个人的规则是否一致 若仅仅是先后的差别 我们可用dp解决一般思考一个子状态 对于当前的那个状态 我们进行什么样的操作 已知什么

  8. .net core使用MQTT

    废话不多说,我们来直接实践…… 一.搭建mqtt控制台服务端 新建一个.net core控制台项目,然后使用Nuget添加MQTTnet包,我这里使用2.4版本,注意不同版本,代码写法不相同,如下图 ...

  9. JVM系列(1)- JVM常见参数及堆内存分配

    常见参数配置 基于JDK1.6 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收模式 -XX:+PrintGCDetails 打印更详细的GC日志 ...

  10. 【POJ - 2229】Sumsets(完全背包)

    Sumsets 直接翻译了 Descriptions Farmer John 让奶牛们找一些数加起来等于一个给出的数N.但是奶牛们只会用2的整数幂.下面是凑出7的方式 1) 1+1+1+1+1+1+1 ...