CodeForces 700B Connecting Universities
统计每一条边的贡献,假设$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的更多相关文章
- 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 ...
- Codeforces 700B Connecting Universities(树形DP)
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...
- codeforces 700B Connecting Universities 贪心dfs
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献 ...
- Codeforces 701E Connecting Universities 贪心
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...
- 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 ...
- 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 ...
- codeforces 701E E. Connecting Universities(树的重心)
题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...
- Connecting Universities
Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...
- codeforces 701 E. Connecting Universities(树+ 边的贡献)
题目链接:http://codeforces.com/contest/701/problem/E 题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行 ...
随机推荐
- js获取菲波那契数列的第N个元素
菲波那契数列,大致可以描叙为a(n) = a(n-1) + a(n-2) (a >=2).类似于这样[1, 1, 2, 3, 5, 8, 13 ...]. 具体大家可以百度一下.下面我们来用js ...
- Docker环境下如何安装Zookeeper
第一步:首先下载Zookeeper的镜像文件: 从仓库中pull 这个zookeeper镜像:docker pull jplock/zookeeper:3.4.8 然后docker images查看该 ...
- 转载一篇nm命令使用的文章,虽然没用用这个方法解决但是文章很好
http://blog.csdn.net/acs713/article/details/13505931
- JS 常用功能收集
JS 常用效果收集 1. 回到顶部>> 爱词霸
- chrome浏览器如何在本地安装谷歌访问助手教程
许多用户都需要使用谷歌的gmail,搜索.我们目前可以用谷歌访问助手解决google无法访问的问题.那么谷歌访问助手在chrome浏览器中如何安装和使用呢?今天我们详细介绍. 本地安装谷歌访问助手的步 ...
- 跨线程传递栈变量带来异常指针Crash
在手Q动漫的一份古老的代码中,现网发现少数crash,错误代码示例: char str[100] = "hello"; dispatch_async(dispatch_get_ma ...
- ManualResetEvent和AutoResetEvent的区别
在讨论这个问题之前,我们先了解这样一种观点,线程之间的通信是通过发信号来进行沟通的.(这不是废话) 先来讨论ManualResetEvent,讨论过程中我会穿插一些AutoResetEvent的内容, ...
- torisegit 保存帐号密码
设置 -> git 编辑本地 .git/config 增加 [credential] helper = store
- jQuery中操作Ajax方法小结
有时候,越深入去了解一个点,越发觉得自己无知,而之前当自己晓得一两个片面的点还洋洋自得,殊不知,这是多么讽刺 jQery对Ajax操作进行了封装,常见的 ajax()属于最底层的方法,使用频率很高的 ...
- 新Mac 开机启动MySQL/MongoDB/Redis 等服务
在Mac上我们使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)来安装和管理开发工具包,例如:mysql.php.redis.只需要一个命令 brew ...