BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
2017-09-19 17:28
525 查看
http://www.lydsy.com/JudgeOnline/problem.php?id=2097
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 417 Solved: 213
[Submit][Status][Discuss]
Description
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)连接。 我们来看看如下的例子:线性的路径集合(7个顶点的树) 1---2---3---4---5---6---7 如果FJ可以封锁两条道路,他可能的选择如下: 1---2 | 3---4 | 5---6---7 这样最长的直径是2,即是最优答案(当然不是唯一的)。Input
* 第1行: 两个空格分隔的整数V和S * 第2...V行: 两个空格分隔的整数A_i和B_iOutput
* 第1行:一个整数,表示FJ可以获得的最大的直径。Sample Input
7 26 7
3 4
6 5
1 2
3 2
4 5
Sample Output
2
HINT
Source
二分最小的直径
验证时,统计出每个节点所在链的长度,如果比当前ans大,就砍去、
#include <algorithm> #include <cstring> #include <cstdio> #define min(a,b) (a<b?a:b) inline void read(int &x) { x=0; register char ch=getchar(); for(; ch>'9'||ch<'0'; ) ch=getchar(); for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0'; } const int N(1e5+5); int n,p,u,v,s,t; int head ,sumedge; struct Edge { int v,next; Edge(int v=0,int next=0):v(v),next(next){} }edge[N<<1]; inline void ins(int u,int v) { edge[++sumedge]=Edge(v,head[u]); head[u]=sumedge; edge[++sumedge]=Edge(u,head[v]); head[v]=sumedge; } int l,r,mid,ans,tmp,cnt,td ,dis ; void DFS(int u,int fa,int num) { dis[u]=0; for(int i=head[u]; i; i=edge[i].next) if(edge[i].v!=fa) DFS(edge[i].v,u,num); cnt=0; for(int i=head[u]; i; i=edge[i].next) if(edge[i].v!=fa) td[++cnt]=dis[edge[i].v]+1; std::sort(td+1,td+cnt+1); for(; cnt&&td[cnt]+td[cnt-1]>num; ) cnt--,tmp++; dis[u]=td[cnt]; } bool check(int x) { tmp=0; DFS(1,0,x); return tmp<=p; } int Presist() { // freopen("longnosee.in","r",stdin); // freopen("longnosee.out","w",stdout); read(n); read(p); for(int u,v,i=1; i<n; ++i) read(u),read(v),ins(u,v); for(l=1,r=n; l<=r; ) { mid=l+r>>1; if(check(mid)) { ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); return 0; } int Aptal=Presist(); int main(){;}
相关文章推荐
- [BZOJ2097][Usaco2010 Dec]Exercise 奶牛健美操(二分+树形dp+贪心)
- [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操
- [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分
- bzoj 2097: [Usaco2010 Dec]Exercise 奶牛健美操 (二分答案+贪心)
- BZOJ 2097 USACO 2010 Dec Gold Exercise 奶牛健美操 二分答案 树形DP 贪心
- BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操
- BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操
- bzoj2097 [Usaco2010 Dec]Exercise 奶牛健美操(二分答案+贪心+树)
- [BZOJ2097][Usaco2010 Dec]Exercise 奶牛健美操(二分+树形dp+贪心)
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分答案+树形dp+贪心
- BZOJ 2097: [Usaco2010 Dec]Exercise 奶牛健美操 二分 贪心
- BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP
- bzoj 2097: [Usaco2010 Dec]Exercise 奶牛健美操【二分+树形dp】
- BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操 二分
- Usaco 2010 Dec Gold Exercise(奶牛健美操)
- BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操 贪心+伪树dp+二分
- BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操
- bzoj2097[Usaco2010 Dec]Exercise 奶牛健美操