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

因为是一棵树,所以一条边把树分成两个集合,假如左边有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. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  2. 欧拉工程第61题:Cyclical figurate numbers

    ---恢复内容开始--- 题目链接 从三角数开始,循环到八角数,再到三角数,求这6个数的和 这个比较复杂,代码在网上找的 Java: package project61; import java.ut ...

  3. SpringMVC学习总结(六)——SpringMVC文件上传例子(2)

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...

  4. org.apache.common.io-FileUtils详解

    org.apache.common.io---FileUtils详解 getTempDirectoryPath():返回临时目录路径; public static String getTempDire ...

  5. Orcle数据库查询练习复习:二

    一.题目 1.找出所有成绩均低于80的学生姓名 select sname from student where sid in( ) select sname from student where si ...

  6. Quartz的misfire特性

    Quartz的misfire特性 只有一个线程.多个job 第一个job产生misfire(executeTime>Interval) 且是repeatForever 那么只会运行第一个job, ...

  7. 简单的SocketExample

    客户端//---------------VerySimpleClient.java package SocketExample; // Tue Nov 2 18:34:53 EST 2004 // / ...

  8. ios中addtarget的用法

    1.addtarget 的.部分使用事件没有直接的操作方式,需要进行调用.就要用addTarget. - (void)setupCustomView { self.customView = [[CHV ...

  9. UIColor的用法

    UIColor,CGColor,CIColor的区别和联系 layer.shadowColor = [UIColor redColor].CGColor; 这个是今天用到的.顺便总结一下. 1.UIC ...

  10. n人比赛,可轮空,比赛轮数和场数

    #include<stdio.h> int chang(int x,int s){ ) return s; ) ; !=){ s+=(x-)/; )/,s); } else{ s+=x/; ...