codeforces 700B Connecting Universities 贪心dfs
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献
因为是一棵树,所以一条边把树分成两个集合,假如左边有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的更多相关文章
- 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 701E Connecting Universities 贪心
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...
- Codeforces 700B Connecting Universities(树形DP)
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...
- CodeForces 700B Connecting Universities
统计每一条边的贡献,假设$u$是$v$的父节点,$(u,v)$的贡献为:$v$下面大学个数$f[v]$与$2*k-f[v]$的较小值. #pragma comment(linker, "/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 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 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 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
随机推荐
- 表单验证插件——validate
表单验证插件——validate 该插件自带包含必填.数字.URL在内容的验证规则,即时显示异常信息,此外,还允许自定义验证规则,插件调用方法如下: $(form).validate({options ...
- 前台将勾选的多个属性放到一个value里面,是一个字符串,传到后台
jq function changeStreet(a){ var valk=$(a).html(); $(a).parents(".select_box").children(&q ...
- ZipArchive框架的文件压缩和解压
导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以) 导入的头文件是#import "Main.h" 文件压缩 -(vo ...
- iOS开发--数组
1.sortedArrayUsingSelector (按Key值大小对NSDictionary排序) NSMutableArray *array = [NSMutableArray arrayWit ...
- 不用Invoke就等用 Control.CheckForIllegalCrossThreadCalls = false;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C#四种文件流的区别(转)
1.FileStream类的读写操作 FileStream类可以对任意类型的文件进行读取操作,而且我们也可以按照需要指定每一次读取字节长度,以此减少内存的消耗,提高读取效率. 代码实例: //创建文件 ...
- Android应用开发学习笔记之Intent
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Intent是什么呢?来看Android官网上的定义: An intent is an abstractdescri ...
- Android开发之EditText属性详解
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- 23.allegro中自动布线[原创]
1. --- 方法①:选择网络自动布线 -- --- 已经步好: --- 方法②: ---- ---- 布线: --- 方法③: -- ----
- 面试题_103_to_124_关于 OOP 和设计模式的面试题
这部分包含 Java 面试过程中关于 SOLID 的设计原则,OOP 基础,如类,对象,接口,继承,多态,封装,抽象以及更高级的一些概念,如组合.聚合及关联.也包含了 GOF 设计模式的问题. 103 ...