分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献

因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校

贪心地想,让每条边在学校的路径上最多,所以贡献为min(x,y)

具体实现:一次dfs即可,复杂度O(N)

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int N = 2e5+;
const int INF = 0x3f3f3f3f;
const LL mod = 1ll<<;
int head[N],tot,sum[N],n,k;
struct Edge{
int v,next;
}edge[N<<];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
LL ans;
void dfs(int u,int f){
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f)continue;
dfs(v,u);
ans+=min(k-sum[v],sum[v]);
sum[u]+=sum[v];
}
}
int main(){
scanf("%d%d",&n,&k);k<<=;
memset(head,-,sizeof(head));
for(int i=;i<=k;++i){
int u;scanf("%d",&u);
++sum[u];
}
for(int i=;i<n;++i){
int u,v;scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
printf("%I64d\n",ans);
return ;
}

codeforces 700B Connecting Universities 贪心dfs的更多相关文章

  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 701E Connecting Universities 贪心

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

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

  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 701E E. Connecting Universities(树的重心)

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

  8. Connecting Universities

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

  9. codeforces 704B - Ant Man 贪心

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

随机推荐

  1. POJ2513Colored Sticks

    http://poj.org/problem?id=2513 题意 : 一些木棒,两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路 : 这个题的话就 ...

  2. Ubuntu环境下利用ant编译nutch2.2.1 & 配置nutch2.2.1

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  3. MySQL数据导出导入【转】

    MySQL基础 关于MySQL数据导出导入的文章,目的有二: 1.备忘 2.供开发人员测试 工具 mysqlmysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$US ...

  4. selenium2 安装、简单使用及浏览器启动问题解决汇总

    一.selenium2 安装 1.安装jdk并配置环境变量 jdk需要1.6版本及以上的,这个从网上下载就可以,环境变量的配置前边的随笔整理过就不多说了. 2.安装Firefox,Selenium I ...

  5. JavaWeb项目开发案例精粹-第3章在线考试系统-001设计

    1. 2. 3. 4. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL ...

  6. 持久化框架Hibernate 开发实例(二)

    1 简述 通过使用Hibernate框架,开发者可以使用面向对象的方式来进行数据库访问,从而取代 以前使用JDBC进行数据库访问的方式.通过使用Hibernate框架,web应用可以通过面向 对象的方 ...

  7. Android 在Intent中传递接口

    总结:在Activity中不能用intent传递匿名接口,原因如下:Activity A中生成了匿名接口M, 这个接口的引用就在组Activity A中,Activity A会禁止接口M 序列化.因为 ...

  8. 我的第一个jquery插件:下拉多选框

    <!DOCTYPE HTML> <html> <head> <title> New Document </title> <meta n ...

  9. 【C#设计模式——创建型模式】简单工场模式

    进入码农行列也有一年半载了,仍然感觉自己混混沌沌,无所事事,无所作为,,,想想都下气,下气归下气,仍要奋起潜行,像愤怒的小鸟一边又一遍的冲向猪头也好,像蜗牛一样往前蹭也罢,总之要有蚂蚁啃骨头的精神!! ...

  10. Lists of network protocols

    https://en.wikipedia.org/wiki/Lists_of_network_protocols Protocol stack: List of network protocol st ...