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个学校进行 ...
随机推荐
- zoom:1的作用
兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. 如下使用DIV做一行两列 ...
- Python第一天——入门Python(4)字典的常用操作
# dic={[1,2,3]:'123'} #可变类型不能当做字典的key,value可以使用任意类型 # dic={(2,3,4):'123'} # print (dic[(2,3,4)]) #元组 ...
- JavaScript星级评分
事件onmouseover <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- Redmine插件及使用
Plugins Plugin list A full list of available Redmine plugins can be found at the Plugin Directory. M ...
- orm2
数据库连接 var orm = require("orm"); orm.connect("mysql://username:password@host/database& ...
- Spring 注解式Aop 入门
首先在spring配置文件中加上 xmlns:aop="http://www.springframework.org/schema/aop" http://www.springfr ...
- Java中IO流
* IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流的方式 * Java用于操作流的类都在IO包中 * 流按流向分为两种:输入流,输出流. * 流按操作类型分为两种: * 字节流 : ...
- LoRaWAN_stack移植笔记(一)--RF硬件相关
和硬件相关的问题 TCXO 的使用 根据SX1276数据手册, 如果使用TCXO,则需要配置RegTcxo寄存器为0x19,代码如下 ``` c void SX1276SetTcxoConfig(vo ...
- 使用 JUnit 报错 java.lang.Exception: No runnable methods
错误详情如下: java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validat ...
- 4-Bom&Dom总结篇
其实Bom就是指浏览器的东西,比如弹窗啊.浏览器信息啊等 而Dom则是指文档的东西,就是浏览器里边html的东西,如元素啊.属性啊.事件什么的 但Bom的唯一顶层对象window又包含Dom的顶层对象 ...