BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操

网友投稿 656 2022-10-22

BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操

BZOJ 2097 [Usaco2010 Dec]Exercise 奶牛健美操

​​‎ 题目描述

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 SLines 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 犯了个错误 把全局变量当局部变量用了 看了题解 发现自己想复杂了 于是重新抄了下题解

求最大值最小二分来做 每次二分我树中最大直径是多少 看要求满足这个条件我至少需要划分几个连通块 那么每次在dfs的时候贪心的维护一下我需要最少划分几次即可 然后每次用这个值去比较一下即可 维护一个d[i]表示我这个子树没有被切割 剩余部分的最长延伸 然后每次更新上去即可

#include#include#define N 110000using namespace std;inline char gc(){ static char now[1<<16],*S,*T; if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;} return *S++;}inline int read(){ int x=0,f=1;char ch=gc(); while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();} while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc(); return x*f;}struct node{ int y,next;}data[N<<1];int num,h[N],q[N],mid,n,m,d[N],s;inline void insert1(int x,int y){ data[++num].y=y;data[num].next=h[x];h[x]=num; data[++num].y=x;data[num].next=h[y];h[y]=num;}inline void dfs(int x,int fa){ int cnt=0;d[x]=1; for (int i=h[x];i;i=data[i].next){ int y=data[i].y;if (y==fa) continue;dfs(y,x); } for (int i=h[x];i;i=data[i].next){ int y=data[i].y;if (y==fa) continue;q[++cnt]=d[y]; } sort(q+1,q+cnt+1); for (int i=cnt;i;--i) if (q[i]+q[i-1]>mid) ++s;else {d[x]=q[i]+1;return;}}int main(){ freopen("bzoj2097.in","r",stdin); n=read();m=read(); for (int i=1;i>1;s=0;dfs(1,0); if (s>m) l=mid+1;else r=mid-1; }printf("%d",l); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:loj6015「网络流 24 题」星际转移
下一篇:【luogu2850】[USACO06DEC]虫洞Wormholes
相关文章

 发表评论

暂时没有评论,来抢沙发吧~