[USACO 2011 Dec Gold] Cow Calisthenics【二分】
Problem 1: Cow Calisthenics [Michael Cohen, 2010] Farmer John continues his never-ending quest to keep the cows fit
by having them exercise on various cow paths that run through the
pastures. These cow paths can be represented as a set of vertices
connected with bidirectional edges so that each pair of vertices
has exactly one simple path between them. In the abstract, their
layout bears a remarkable resemblance to a tree. Surprisingly, each
edge (as it winds its way through the pastures) has the same length. For any given set of cow paths, the canny cows calculate the longest
possible distance between any pair of vertices on the set of cowpaths
and call it the pathlength. If they think this pathlength is too
large, they simply refuse to exercise at all. Farmer John has mapped the paths and found V (2 <= V <= 100,000)
vertices, conveniently numbered from 1..V. In order to make shorter
cowpaths, he can block the path between any two vertices, thus
creating more sets of cow paths while reducing the pathlength of
both cowpath sets. Starting from a single completely connected set of paths (which
have the properties of a tree), FJ can block S (1 <= S <= V-1)
paths, creating S+1 sets of paths. Your goal is to compute the best
paths he can create so that the largest pathlength of all those
sets is minimized. Farmer John has a list of all V-1 edges in his tree, each described
by the two vertices A_i (1 <= A_i <= V) and B_i (1 <= B_i <= V; A_i
!= B_i) that it connects. Consider this rather linear cowpath set (a tree with 7 vertices): 1---2---3---4---5---6---7 If FJ can block two paths, he might choose them to make a map like
this:
1---2 | 3---4 | 5---6---7 where the longest pathlength is 2, which would be the answer in
this case. He can do no better than this. TIME LIMIT: 2 seconds MEMORY LIMIT: 32 MB PROBLEM NAME: exercise INPUT FORMAT: * Line 1: Two space separated integers: V and S * Lines 2..V: Two space separated integers: A_i and B_i SAMPLE INPUT (file exercise.in): 7 2
6 7
3 4
6 5
1 2
3 2
4 5 OUTPUT FORMAT: * Line 1: A single integer that is the best maximum pathlength FJ can
achieve with S blocks SAMPLE OUTPUT (file exercise.out): 2
不懂英文自行解决。。。
一看到最大值最小,就是二分,可是想了半天也没想出来如何check。看了题解,恍然大悟。
随便选一个根拉成一棵树,然后对于以r为根的子树,假设以其儿子节点为根的子树中该断的边已经断了,那么对于以r为根的子树中,其直径为“最深”的两个儿子i与j的深度之和 + 2,“最深”的意思是,以这两个节点往下走能走的最深。如果这个值大于二分的那个最大值,则最深的那个儿子就要断开与r的连接,这样直到“最深”的两个儿子i与j的深度之和 + 2 <= 二分的最大值。当有一个节点时同理,反正剩下就是细节了。
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm> const int maxn = 100005; int n, s, t1, t2, ans_s, root, biaozhun;
int head[maxn], to[maxn << 1], next[maxn << 1], lb;
std::vector<int> son[maxn]; inline void ist(int aa, int ss) {
to[lb] = ss;
next[lb] = head[aa];
head[aa] = lb;
++lb;
}
bool cmp(int aa, int ss) {
return aa > ss;
}
int dfs(int r, int p) {
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != p) {
son[r].push_back(dfs(to[j], r));
}
}
std::sort(son[r].begin(), son[r].end(), cmp);
if (son[r].size() > 1) {
int lmt = son[r].size();
int i;
for (i = 0; i < lmt - 1; ++i) {
if (son[r][i] + son[r][i + 1] + 2 <= biaozhun) {
break;
}
++ans_s;
}
if (i == lmt - 1) {
if (son[r][i] + 1 > biaozhun) {
++ans_s;
return 0;
}
else {
return son[r][i] + 1;
}
}
else {
return son[r][i] + 1;
}
}
else if (son[r].size() == 1) {
if (son[r][0] + 1 > biaozhun) {
++ans_s;
return 0;
}
else {
return son[r][0] + 1;
}
}
else {
return 0;
}
}
inline bool check(int mx) {
ans_s = 0;
biaozhun = mx;
for (int i = 1; i <= n; ++i) {
son[i].clear();
}
dfs(root, 0);
return ans_s <= s;
} int main(void) {
freopen("exercise.in", "r", stdin);
freopen("exercise.out", "w", stdout);
memset(head, -1, sizeof head);
memset(next, -1, sizeof next);
unsigned seed;
scanf("%d%d", &n, &s);
seed += n + s;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &t1, &t2);
seed += t1 + t2;
ist(t1, t2);
ist(t2, t1);
}
srand(seed);
root = rand() % n + 1; int left = 0, right = n, mid;
while (left != right) {
mid = (left + right) >> 1;
if (check(mid)) {
right = mid;
}
else {
left = mid + 1;
}
}
printf("%d\n", left);
return 0;
}
[USACO 2011 Dec Gold] Cow Calisthenics【二分】的更多相关文章
- [USACO 2011 Nov Gold] Cow Steeplechase【二分图】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...
- [USACO 2011 Dec Gold] Threatening Letter【后缀】
Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and w ...
- [USACO 2017 Dec Gold] Tutorial
Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...
- BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- Usaco 2010 Dec Gold Exercise(奶牛健美操)
/*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...
- [USACO 2012 Feb Gold] Cow Coupons【贪心 堆】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=118 传送门2:http://www.lydsy.com/JudgeOn ...
- [USACO 2011 Nov Gold] Above the Median【逆序对】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...
- bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=110 传送门2:http://www.lydsy.com/JudgeOn ...
- USACO 2011 February Silver Cow Line /// 康拓展开模板题 oj22713
题目大意: 输入n k,1-n的排列,k次操作 操作P:输入一个m 输出第m个排列 操作Q:输入一个排列 输出它是第几个排列 Sample Input 5 2P3Q1 2 5 3 4 Sample O ...
随机推荐
- 关于maven多个模块的build顺序 [INFO] Reactor Build Order
对于一个maven项目,如果有多个模块,那么它们的执行顺序是什么样的呢? 在执行mvn操作的时候,你可以看到如下信息,这个便是maven的build顺序 那么maven是如何决定顺序的呢?如下: 在多 ...
- Objective-c写冒泡排序
做面试题遇到用obj-c写冒泡排序,随便写了个 - (NSMutableArray *)sorted:(NSMutableArray *)array { int len = [array count] ...
- windows下的两个等待函数
windows下的两个等待技术 第一种: Win32 Sleep()函数 这个函数要求操作系统中止线程动作.直到读过某个指定的时间之后才恢复.能在某个线程结束时(而不是某段时间结束时)被调 ...
- UISlider无法拖动进度条的问题解决
UISlider无法拖动进度条的问题解决 最近业务中的视频播放使用到了UISlider,但是有一个奇怪的问题,就是在Modar出来的控制器中UISlider是可以正常使用的,但是在Push出来的控制器 ...
- Eclipse+Maven(webapp)+Jetty+JReBel的配置方法
maven配置 省略 jrebel配置 jrebel毋须繁琐的配置,把jrebel-5.6.3-crack.zip解压放在磁盘文件夹就可以.(笔者路径为:D:\coding-life\IDE\jreb ...
- [IT练习册]Python练习项目 思路
1.爬虫:爬取如下网站一年的内容. http://www.calvarymoravian.org/dailytext 2.蛇形棋: 开发一个类似蛇形棋的游戏.最好基于Web. 3.爬虫+通讯录: 从公 ...
- 将最大主机/ DNS名称字符长度从63增加到255
https://mp.weixin.qq.com/s/WcjaAgAOvEhjtP2nXx5Fhw Zabbix 4.0.0alpha8发行说明 运维帮 昨天 Zabbix团队很高兴地宣布Zabbix ...
- 前端富文本 js 版本
https://s3.pstatp.com/pgc/v2/resource/tt_ueditor_v3_temple/tt-editor.all.js?20180425
- Mac Mysql [ERR] 2006 - MySQL server has gone away
Mac mysql 安装后,导入sql数据,出现这个错误: 处理方式,是因为sql文件太大,需要修改mysql的配置.如果没有my.cnf就自己建一个. cd /etc sudo vim my.cnf ...
- YTU 2558: 游起来吧!超妹!
2558: 游起来吧!超妹! 时间限制: 1 Sec 内存限制: 128 MB 提交: 70 解决: 22 题目描述 夏天到了,无聊的超妹跑到了落雪湖里抓鱼吃.结果,游到湖的正中 央时被湖边保安看 ...