【EOJ3654】管理孩子(贪心,二分)
题意:有一棵n个点的树,其中有k个是关键点,将树分割成若干部分,每部分至少包含一个关键点,求最大分割大小的最小值

思路:
最后特判一下f[1]的值
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define mem0(a) memset(a,0,sizeof(a))
#define N 210000
#define M 130
#define MOD 1000000007
#define eps 1e-8
#define pi acos(-1)
#define oo 1000000000 int flag[N],f[N],a[N],n,mx;
vector<int>c[N]; void dfs(int u)
{
flag[u]=;
if(a[u])
{
int t=;
for(int i=;i<=(int)c[u].size()-;i++)
{
int v=c[u][i];
if(!flag[v])
{
dfs(v);
if(f[v]<) t-=f[v];
}
}
f[u]=t+;
}
else
{
int t=,s=oo;
for(int i=;i<=(int)c[u].size()-;i++)
{
int v=c[u][i];
if(!flag[v])
{
dfs(v);
if(f[v]<) t-=f[v];
else s=min(s,f[v]);
}
}
if(mx-s>=t+) f[u]=s+t+;
else f[u]=-t-;
}
} int isok(int m)
{
mem0(flag);
mem0(f);
mx=m;
dfs();
int flag=;
if(f[]<) return ;
for(int i=;i<=n;i++)
if(abs(f[i])>m) return ;
return ;
} int main()
{
int k;
scanf("%d%d",&n,&k);
for(int i=;i<=n-;i++)
{
int x,y;
scanf("%d%d",&x,&y);
c[x].push_back(y);
c[y].push_back(x);
}
for(int i=;i<=k;i++)
{
int x;
scanf("%d",&x);
a[x]=;
}
int l=,r=n,last=n;
while(l<=r)
{
int mid=(l+r)>>;
if(isok(mid)){last=mid; r=mid-;}
else l=mid+;
}
printf("%d\n",last);
return ;
}
【EOJ3654】管理孩子(贪心,二分)的更多相关文章
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- Card Game Cheater(贪心+二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- LightOj1383 - Underwater Snipers(贪心 + 二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1383 题意:在平面图中,有一条河,用直线y=k表示,河上面(y>k)的都是敌方区 ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
随机推荐
- 转MySQL详解--索引
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将 ...
- Datetime与Datetime2的区别
Datetime: 时间格式,对应于数据库中的DateTime类型,对应于.NET里面的System. DateTime类型.DateTime支持日期从1753年1月1日到9999年12 ...
- How to Upload multiple files to documentLibrary in one time
In a Sharepoint 2013 website,we can upload one file to the documentlibrary by click "Uploa ...
- html简单的分享功能
超级简单的分享. 包括:QQ.QQ空间.新浪微博.腾讯微博,微信(只是一个二维码): 1.首先是html代码: (前端我并不太会,一直用的都是bootstrap) <div class=&quo ...
- 【目录】Spring 源码学习
[目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...
- rails 中 preload、includes、Eager load、Joins 的区别
Rails 提供了四种不同加载关联数据的方法.下面就来介绍一下. 一.Preload Preload 是以附加一条查询语句来加载关联数据的 User.preload(:posts).to_a # =& ...
- CDateTimeUI类源码分析
CDateTimeUI是duilib里选择日期的控件,继承于CLabelUI控件,用于记录已经选择的日期,选择控件则是调用win32的日期选择控件. CDateTimeUI包含两个类,一个是CDate ...
- Linux环境搭建系列之sorl服务器的安装部署
http://blog.csdn.net/upxiaofeng/article/details/51425732
- 接口自动化测试框架Karate入门
介绍 在这篇文章中,我们将介绍一下开源的Web-API自动化测试框架--Karate Karate是基于另一个BDD测试框架Cucumber来建立的,并且共用了一些相同的思想.其中之一就是使用Gher ...
- 阿里云DTS VS MySQLdump
云平台的到来,使得越来越多用户的数据库由云下迁到云上.对于这种情况,阿里对此提出两种方案,一种是MySQL自带的MySQLdump,另外一种就是阿里云的DTS. DTS支持异构数据源之间的数据迁移同步 ...