题目描述

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 ;
}
 
 
 

最新文章

  1. logrotate机制与原理[转载]
  2. codeforces C. Arithmetic Progression 解题报告
  3. Shell 总结
  4. CSS 浏览器默认样式
  5. easy ui 学习笔记,不断整理中............
  6. MySQL根据出生日期计算年龄
  7. LinkedList源码阅读笔记(1.8)
  8. win32-api: 让 static 控件中的水平横行,垂直居中。
  9. 手动卸载Office2010
  10. 【转】OPPO A77保持应用后台运行方法
  11. (原)torch7中指定可见的GPU
  12. k8s官方安装版本
  13. 关于android 调用网页隐藏地址栏
  14. B-spline Curves 学习之B样条基函数计算实例(3)
  15. declare命令
  16. 查询出menupath字段中 出现 “- &quot;(横杆)大于3次的 记录
  17. 利用Instrument Leak来发现App中的内存泄露
  18. Hibernate 一对多,多对多,多对一检索策略
  19. notepad++配置编译运行java
  20. 多线程(八)~ThreadLocal、InheritableThreadLocal的使用

热门文章

  1. day 21 今日学习内容
  2. lwip Packet buffers (PBUF) API 操作 集合
  3. php输出js到前端
  4. [Lydsy1805月赛]对称数 BZOJ5361
  5. Python 调用 Redis API
  6. java 读取excel内容转为JSONArray
  7. 现有工程中集成Cordova
  8. 使用redis
  9. 2017-2018 Exp1 PC平台逆向破解 20155214
  10. 20155216 Exp3 免杀原理与实践