I guess I'm going to keep doing water problems and dogs.
It's like a mock game, greedy for a bit.
1#include <bits/stdc++.h>2 using namespacestd;3 intRead () {4 intx=0, f=1;CharCh=GetChar ();5 while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}6 while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}7 returnx*F;8 }9 #defineN 100005Ten intn,m,f[n],q[n],cnt; One structnode{ A intTo,next; -}e[n<<1]; - intTot,head[n]; the voidAddintXinty) { -E[++tot]= (Node) {y,head[x]};head[x]=tot; -E[++tot]= (Node) {x,head[y]};head[y]=tot; - } + voidDfsintXintFaintLim) { -f[x]=0; + intt=0; A for(intI=head[x];i;i=e[i].next)if(e[i].to!=FA) Dfs (E[i].to,x,lim); at for(intI=head[x];i;i=e[i].next)if(e[i].to!=FA) { -q[++t]=f[e[i].to]+1; - } -Sort (q+1, q+1+t); - while(t&&q[t]+q[t-1]>lim) cnt++,t--; -f[x]=Q[t]; in return; - } to BOOLCheckintx) { +Cnt=0;d FS (1,0, x); - if(cnt<=m)return 1;Else return 0; the } * intMain () { $N=read (); m=read ();Panax Notoginseng for(intI=1; i<n;i++) Add (read (), read ()); - intL=1, r=n-1, Ans,mid; the while(l<=R) { +Mid=l+r>>1; A if(Check (mid)) r=mid-1, ans=mid; the ElseL=mid+1; + } -printf"%d\n", ans); $}
View Code 2097: [Usaco2010 dec]exercise Cow aerobics time limit: 10 sec memory limit: 64 MB
submit: 259 solved: 129
[Submit][status][discuss] Descriptionfarmer John to keep the cows healthy, let the poor cows keep running on the path between the pastures. The set of paths for these cows can be represented as a set of points and a number of bidirectional paths connecting two vertices, so that there is exactly one simple path between each pair of points. In short, the layout of these points is a tree, and each edge is equal to 1. For a given set of cow paths, the astute cows calculate the maximum of any point-to-path, which we call the diameter of the path set. If the diameter is too large, the cows will refuse to exercise. Farmer John Marks each point as 1. V (2 <= v <= 100,000). To get a shorter diameter, he can choose to block some existing roads so that more paths can be set, reducing the diameter of some path sets. We start with a tree, FJ can choose to block S (1 <= s <= V-1) bidirectional Road, thus obtaining a s+1 path collection. All you have to do is figure out the best blocking scheme so that he gets the maximum size of all the path sets as small as possible. Farmer John tells you all V-1 bidirectional roads, each expressed as: Vertex a_i (1 <= a_i <= v) and b_i (1 <= b_i <= v; a_i!= b_i) connection. Let's take a look at the following example: Linear path Set (Tree of 7 vertices) 1---2---3---4---5---6---7 if FJ can block two roads, he may choose the following: 1---2 | 3---4 | 5---6---7 the longest diameter is 2, which is the best answer (not the only one, of course). input* line 1th: Two spaces separated by an integer V and S * 2nd ... V Line: Two space-delimited integers a_i and b_ioutput* line 1th: An integer that represents the maximum diameter that FJ can obtain. Sample Input7 2
6 7
3 4
6 5
1 2
3 2
4 5
Sample Output
2
bzoj2097[usaco2010 Dec] Cow aerobics