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个学校进行 ...
随机推荐
- 年末整理git和svn的使用心得
实习加毕业工作也一年多了,用过svn 也用过git,现在也是两种版本管理工具交替不同的项目再用. 趁年末放假之际,来梳理下. 对于SVN常用命令: .svn cp svn-trunk地址 svn-br ...
- spring-mvc + shiro框架整合(sonne_game网站开发04)
这篇文章讲的内容是在之前spring + mybatis + spring-mvc + freemarker框架整合的代码的基础上.有需要的可以看看我博客的前两篇文章. 另外,本文章所讲相关所有代码都 ...
- python命令调用函数os.popen
参考自xerosploit 描述:利用os.popen()函数调用系统命令nmap进行扫描,并用grep命令对扫描结果关键内容进行提取 代码 #!/usr/bin/env pthon #--*--co ...
- 隐藏或删除指定的html元素
<div id="Contain"> <div>好好学习<div> <div>天天向上<div> <div> ...
- svn问题(队列)
svn执行clean up命令时报错"Previous operation has not finished; run 'cleanup' if it was interrupted&quo ...
- maven项目如何引用本地的jar包
下载该jar包到本地(如下载目录结构为:D:\Users\lu.wang\Downloads\searchservice\searchservice\jar\ttd.search.searchserv ...
- C语言 · 分解质因数
问题描述 求出区间[a,b]中所有整数的质因数分解. 输入格式 输入两个整数a,b. 输出格式 每行输出一个数的分解,形如k=a1*a2*a3...(a1<=a2<=a3...,k也是从小 ...
- Unity3D消息:消息传递函数
- mysql连接失败或出现“Too many connections”错误
mysql连接失败或出现"Too many connections"错误 # 按自己服务器的配置文件路径修改 vi /etc/my.cnf 查找:max_connections 修 ...
- shell中的特殊符号
Shell符号及各种解释对照表: Shell符号 使用方法及说明 # 注释符号(Hashmark[Comments]) 1.在shell文件的行首,作为shebang标记,#!/bin/bash; 2 ...