【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 ...
随机推荐
- java容器操作一
List l = new ArrayList(); l.add(1); l.add("ne"); // 获取 System.out.println(l.get(0)); // 判断 ...
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- OSI七层模型加协议
OSI七层网络模型 TCP/IP四层概念模型 对应网络协议 应用层(Application) 应用层 HTTP.TFTP, FTP, NFS, WAIS.SMTP 表示层(Presentation) ...
- jsp 添加jstl标签
jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...
- 「暑期训练」「基础DP」 Piggy-Bank (HDU-1114)
题意与分析 完全背包问题. 算法背包九讲里面都有提到过,我自己再说下对完全背包的理解. 为什么01背包中遍历状态从VV到00?考虑一下基本方程$dp[i][j]=max(dp[i-1][j-w[i]] ...
- 【个人训练】The Cow Lexicon(POJ-3267)
继续大战dp.2018年11月30日修订,更新一下现在看到这个题目的理解(ps:就现在,poj又503了). 题意分析 这条题目的大意是这样的,问一字符串内最少删去多少的字符使其由给定的若干字符串构成 ...
- idea在Maven Projects中显示灰色的解决办法
问题描述: 在使用idea的过程中,遇到其中一个maven模块变成灰色,如下所示: 问题解决: 造成这个的原因可能是忽略了maven模块. 可以尝试如下解决方法:在idea中进入Settings–&g ...
- Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)
作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...
- 2015年开源前端框架盘点TOP20
2015年,榜单根据github上star数作为排名依据.(榜单中大部分为组件式框架, react.Angular等基础框架不在此篇讨论) 1.Bootstrap 类别/语言:HTML.CSS.Jav ...
- 一个简单的NetCore项目:2 - 登录
1-UI,登陆界面布局 PS:使用的是metronic 框架,没有用过的可以自行百度. 1.1 metronic 放在wwwroot文件夹下面 1.2 metronic 中的 open sans 使 ...