【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 ...
随机推荐
- guacamole实现上传下载
目录 1. 源码解读 2. 上传下载的核心代码 分析的入手点,查看websocket连接的frame 看到首先服务端向客户端发送了filesystem请求,紧接着浏览器向服务端发送了get请求,并且后 ...
- 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃
我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...
- BZOJ 4029 HEOI2015 定价 数位贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4029 题意概述:对于一个数字的荒谬程度定义如下:删除其所有的后缀0,然后得到的数字长度为a ...
- URAL 1736 Chinese Hockey(网络最大流)
Description Sergey and Denis closely followed the Chinese Football Championship, which has just come ...
- C++STL——队列
一.相关定义 原理:queue 队列也是一个线性存储表,元素数据的插入在表的一端进行,在另一端删除,从而构成了一个先进先出FIFO(First In First Out)表. 队头&队尾:插入 ...
- Week1 Team Homework #1 from Z.XML-对于学长项目《shield star》的思考和看法
试用了一下学长黄杨等人开发的<shield star>游戏. 其实作为一个学弟,我对cocos2d-x引擎还算是比较了解,所以对于这样一款很“典型 ...
- 【转】C++操作符的优先级 及其记忆方法
优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的操作符作用域操作符后置自增操作 ...
- 解决Mysql错误Too many connections的方法
MySQL数据库 Too many connections出现这种错误明显就是 mysql_connect 之后忘记 mysql_close:当大量的connect之后,就会出现Too many co ...
- elementUI的导航栏怎么根据路由默认选中相关项
1. <el-menu :default-active="this.$route.path.substr(1)" class="left-nav"> ...
- [C/C++] C++常见面试题
参考:http://blog.csdn.net/shihui512/article/details/9092439 1.new.delete.malloc.free之间的关系 malloc和free都 ...