题目描述

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

Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑。这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径。简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1。 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径。如果直径太大,奶牛们就会拒绝锻炼。 Farmer John把每个点标记为1..V (2 <= V <= 100,000)。为了获得更加短 的直径,他可以选择封锁一些已经存在的道路,这样就可以得到更多的路径集合, 从而减小一些路径集合的直径。 我们从一棵树开始,FJ可以选择封锁S (1 <= S <= V-1)条双向路,从而获得 S+1个路径集合。你要做的是计算出最佳的封锁方案,使得他得到的所有路径集合 直径的最大值尽可能小。 Farmer John告诉你所有V-1条双向道路,每条表述为:顶点A_i (1 <= A_i <= V) 和 B_i (1 <= B_i <= V; A_i!= B_i)连接。

输入输出格式

输入格式:

* Line 1: Two space separated integers: V and S

* Lines 2..V: Two space separated integers: A_i and B_i

输出格式:

* Line 1: A single integer that is the best maximum pathlength FJ can achieve with S blocks

输入输出样例

输入样例#1:

7 2
6 7
3 4
6 5
1 2
3 2
4 5
输出样例#1:

2

Solution:

  本题二分答案+贪心。

  模拟考试的T3,思路不是很难。

  我们二分子树的最大直径$mid$,然后遍历原树,将当前根节点的子节点到叶子节点的距离$dis[x]$进行从大到小排序,贪心的删掉最大边,直到最大的两个$dis$值相加不大于$mid$为止(不合法情况各耗费一次割边机会),每个子树返回当前合法的最大的$dis$值,对该过程递归,最后比较割边的次数和s。

  那么时间复杂度$O(n\log ^2 n)$。

代码:

/*Code by 520 -- 10.18*/
#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=1e6+;
int n,k,fa[N],to[N],net[N],h[N],cnt;
int dis[N>>],l,r,mid,ans,tot;
int root,rd[N>>];
bool vis[N>>]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt,rd[v]++;} void pre(int u){
for(RE int i=h[u];i;i=net[i])
if(to[i]!=fa[u]) fa[to[i]]=u,pre(to[i]);
} int stk[N],top;
void dfs(int u){
int bot=top;
for(RE int i=h[u];i;i=net[i])
if(to[i]!=fa[u]) {
dfs(to[i]);
stk[++top]=dis[to[i]]+;
}
sort(stk+bot+,stk+top+); int i;
for(i=top;i>bot;i--)
if(stk[i]>mid||i>bot+&&stk[i]+stk[i-]>mid) tot++;
else break;
dis[u]=(i==bot?:stk[i]);
top=bot;
} il bool check(){
tot=,dfs(root);
return tot<=k;
} int main(){
n=gi(),k=gi(); int u,v;
For(i,,n) u=gi(),v=gi(),add(u,v),add(v,u);
root=rand()%n+;
pre(root);
l=,r=n;
while(l<=r){
mid=l+r>>;
if(check()) ans=mid,r=mid-;
else l=mid+;
}
cout<<ans;
return ;
}
 
 
 

P3000 [USACO10DEC]牛的健美操Cow Calisthenics的更多相关文章

  1. 【luoguP3000】 [USACO10DEC]牛的健美操Cow Calisthenics

    题目链接 二分答案,判断需要断几条边,用\(f[i]\)表示以\(i\)为根的子树断边后的最长路径,对于一个点\(u\),存在\(f[v]>mid\)时就删到\(v\)的边\(f[v1]+f[v ...

  2. bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...

  3. bzoj1623 / P2909 [USACO08OPEN]牛的车Cow Cars

    P2909 [USACO08OPEN]牛的车Cow Cars 显然的贪心. 按速度从小到大排序.然后找车最少的车道,查询是否能填充进去. #include<iostream> #inclu ...

  4. bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...

  5. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  6. P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题目描述 Like everyone else, FJ is always thinking up ways to incr ...

  7. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  8. LCA【洛谷P2971】 [USACO10HOL]牛的政治Cow Politics

    P2971 [USACO10HOL]牛的政治Cow Politics 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向 ...

  9. 编程算法 - 最好牛线(Best Cow Line) 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/37909933 最好牛线(Best Cow L ...

随机推荐

  1. 【hdu4405】AeroplaneChess

    题目大意:问从0到n所花费时间平均时间.每次有投骰子,投到几就走几步.原题还有坐飞机 #include<iostream> #include<cmath> #include&l ...

  2. 常用的php数组函数

    以下是自己比较常用的数组函数 数组元素增加减少array_pusharray_poparray_shiftarray_unshift array_splice  (对数组的增删改) array_sli ...

  3. static成员函数不能调用non-static成员函数

    1 一般类静态成员函数不能调用非静态成员函数 2 static成员函数可以调用构造函数吗? 答案是肯定的,由于static成员函数没有this指针,所以一般static成员函数是不能访问non-sta ...

  4. 20155216 Exp6 信息搜集与漏洞扫描

    Exp6 信息搜集与漏洞扫描 实践内容 信息搜集 whois查询 使用whois查询域名注册信息,查询百度服务器(进行whois查询时去掉www等前缀,因为注册域名时通常会注册一个上层域名,子域名由自 ...

  5. 20155331《网络对抗技术》Exp4:恶意代码分析

    20155331<网络对抗技术>Exp4:恶意代码分析 实验过程 计划任务监控 在C盘根目录下建立一个netstatlog.bat文件(先把后缀设为txt,保存好内容后记得把后缀改为bat ...

  6. 20155338 《网络攻防》 Exp7 网络欺诈防范

    20155338 <网络攻防> Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击 在一些公共场所,看到有免费的公用WIFI就想连的时候就容易受到 在日常 ...

  7. WPF后台线程更新UI

    0.讲点废话 最近在做一个文件搜索的小软件,当文件多时,界面会出现假死的状况,于是乎想到另外开一个后台线程,更新界面上的ListView,但是却出现我下面的问题. 1.后台线程问题 2年前写过一个软件 ...

  8. GBDT源码剖析

    如今,GBDT被广泛运用于互联网行业,他的原理与优点这里就不细说了,网上google一大把.但是,我自认为自己不是一个理论牛人,对GBDT的理论理解之后也做不到从理论举一反三得到更深入的结果.但是学习 ...

  9. libgdx学习记录18——Box2d物理引擎

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

  10. shell变量常用方法

    变量之数组操作: 参考网址:http://www.jb51.net/article/55253.htm #直接赋值 [root@local-]=chengd [root@local-]=xrd [ro ...