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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
struct Edge { int u,v,nx; }e[*maxn];
int h[maxn],sz;
int n,m,f[maxn],dis[maxn];
LL sum; void dfs(int x,int fa)
{
for(int i=h[x];i!=-;i=e[i].nx)
{
if(fa==e[i].v) continue;
dfs(e[i].v,x); f[x]=f[x]+f[e[i].v];
}
for(int i=h[x];i!=-;i=e[i].nx)
{
if(fa==e[i].v) continue;
sum=sum+min(f[e[i].v],*m-f[e[i].v]);
}
} void add(int u,int v)
{
e[sz].u=u; e[sz].v=v; e[sz].nx=h[u]; h[u]=sz++;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=*m;i++) { int x; scanf("%d",&x); f[x]=; }
memset(h,-,sizeof h);
for(int i=;i<n;i++)
{
int u,v; scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(,); printf("%lld\n",sum);
return ;
}

CodeForces 700B 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(树形DP)

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

  3. codeforces 700B Connecting Universities 贪心dfs

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

  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. 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 701 E. Connecting Universities(树+ 边的贡献)

    题目链接:http://codeforces.com/contest/701/problem/E 题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行 ...

随机推荐

  1. abstract class与interface的区别与联系

    1.相同点:A. 两者都是抽象类,都不能实例化.B. interface实现类及abstract class的子类都必须要实现已经声明的抽象方法. 2. 不同点:A. interface需要实现,要用 ...

  2. [ios2]OS 6 SDK: 在应用内展示App Store【转】

    出于什么样的原因你会希望用户从你的iOS app中进入App Store呢?可能你想用户去App Store 为你的应用评分,也可能你希望用户看到你其他的iOS app.iOS 6引入了SKStore ...

  3. Mybatis学习笔记(二) 之实现数据库的增删改查

    开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...

  4. Nginx stream(TCP/UDP)负载均衡

    Nginx-1.11.6编译安装 nginx编译安装,(平台:ubuntu 14.04); sudo apt-get install zlib1g-dev sudo apt-get install l ...

  5. 二、spark入门之spark shell:文本中发现5个最常用的word

    scala> val textFile = sc.textFile("/Users/admin/spark-1.5.1-bin-hadoop2.4/README.md") s ...

  6. Ajax beforeSend和complete 方法与防止重复提交

    $.ajax({ beforeSend: function(){ // Handle the beforeSend event }, complete: function(){ // Handle t ...

  7. HashMap和Hashtable的同和不同

    一.综述 可以直接根据hashcode值判断两个对象是否相等吗?肯定是不可以的,因为不同的对象可能会生成相同的hashcode值.虽然不能根据hashcode值判断两个对象是否相等,但是可以直接根据h ...

  8. Tiny6410之重定位代码到SRAM+4096

    重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...

  9. Erlang虚拟机的启动

    Erlang虚拟机的启动 erl实际上是一个shell脚本,设置几个环境变量之后,调用执行erlexec.erlexec的入口点在 otp_src_R15B01/erts/etc/common/erl ...

  10. Oracle 收集统计数据

    查看最新用户表统计信息 select owner,table_name,last_analyzed from dba_tables where owner not like '%SYS%' order ...