Codevs 3279 Cow Aerobics

Source: Internet
Author: User

3279 Cow Aerobics  

Usaco

time limit: 2 s space limit: 256000 KB title level: Diamonds Diamond       Title Description Description

Farmer John kept the poor cows in the pasture to keep the cows healthy .
run on the path. The path collection of these cows can be represented as a point set and some connections
A bidirectional path of two vertices, so that there is exactly one simple route between each pair of points. In a nutshell,
The layout of these points is a tree, and each side is equal to a length of 1.

for a given set of cow paths, smart cows calculate the maximum of any point-to-path ,
We call this path a set of diameters. If the diameter is too large, the cows will refuse to exercise.

Farmer John Marks each point as 1. V (2 <= v <= 100,000). In order to get shorter
the diameter, he can choose to block some already existing roads, so that you can get more paths to the collection,
thereby reducing the diameter of some path sets.

we start with a tree, FJ can choose to block S (1 <= s <= V-1) Two-way road to obtain
s+1 a collection of paths. All you have to do is figure out the best blocking scheme so that he gets a collection of all the paths
the maximum diameter is 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 set of paths (7-vertex tree)

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).

Enter a description input Description

* Line 1th: Two spaces separated by integers V and s

* 2nd ... V Line: Two space-delimited integers a_i and b_i

outputs description output Description

* Line 1th: An integer that represents the maximum diameter that the FJ can obtain.

sample input to sample

9 |

6 7

3 4

6 5

1 2

3 2

4 5

Sample output Sample outputs

2

data size & Hint

For 50% of data, meet v<=100;

For 100% of data, meet v<=100000

/*binary answer + tree DP Codevs A point re is estimated to be a burst stack bzoj.*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#defineMAXN 100010using namespacestd;intn,s,topt,l,r,ans,sum;intFIRST[MAXN];p riority_queue<int>Q[MAXN];structedge{intto ; intNext;} E[MAXN*2];intinit () {intx=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}voidAddintXinty) {topt++; E[topt].to=y; E[topt].next=First[x]; FIRST[X]=topt;}intDfsintXint  from,intlimit) {     while(!q[x].empty ()) Q[x].pop (); Q[x].push (0); Q[x].push (0);  for(intI=first[x];i;i=E[i].next) {        intto=e[i].to; if(to== from)Continue; intHa=DFS (TO,X,LIMIT); Q[x].push (Ha+1); }     while(1)    {        intHa=q[x].top (); Q[x].pop (); intHe=Q[x].top (); if(ha+he>limit) Sum++; Else{q[x].push (ha);  Break; }    }    returnq[x].top ();}intJudgeintx) {Sum=0;d FS (1,0, x); returnsum<=s;}intMain () {n=init (); s=init ();  for(intI=1; i<=n-1; i++)    {        intx, y; X=init (); y=init ();    Add (x, y); add (y,x); } l=0, r=N;  while(l<=r) {intMid= (L+R)/2; if(judge (mid)) {R=mid-1; Ans=mid; }        ElseL=mid+1; } printf ("%d\n", ans); return 0;}
/*The array version actually runs faster and doesn't explode.*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#defineMAXN 100010using namespacestd;intn,s,topt,l,r,ans,sum;intFIRST[MAXN];intF[MAXN],A[MAXN];structedge{intto ; intNext;} E[MAXN*2];intinit () {intx=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}voidAddintXinty) {topt++; E[topt].to=y; E[topt].next=First[x]; FIRST[X]=topt;}voidDfsintXint  from,intlimit) {    inttot=0;  for(intI=first[x];i;i=E[i].next) {        intto=e[i].to; if(to== from)Continue;    DFS (TO,X,LIMIT); }     for(intI=first[x];i;i=E[i].next) {        intto=e[i].to; if(to== from)Continue; a[++tot]=f[to]+1; } sort (A+1, a+tot+1);  while(tot>0&&a[tot]+a[tot-1]>limit) tot--, sum++; F[X]=A[tot];}intJudgeintx) {Sum=0;d FS (1,0, x); returnsum<=s;}intMain () {n=init (); s=init ();  for(intI=1; i<=n-1; i++)    {        intx, y; X=init (); y=init ();    Add (x, y); add (y,x); } l=0, r=N;  while(l<=r) {intMid= (L+R)/2; if(judge (mid)) {R=mid-1; Ans=mid; }        ElseL=mid+1; } printf ("%d\n", ans); return 0;}

Codevs 3279 Cow Aerobics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.