[Dichotomy]

Magic check function,

Any of the ever-changing world, I always check is check

 

1>land

A tree, cut after cut seeking knife,
cut a few trees +. 1, the diameter of the smallest maximum value is the number

// Because the cut with a knife how to cut the maximum diameter related to poor control,
 // so we enumerate the largest diameter, and then go dfs chop sticks 

// Then, let us witness the magic of the half cut it 
#include <cstdio> 
#include <the cstdlib> 
#include <Vector>
 the using  namespace STD;
 int n-, Cut;
 const  int N = 4E5 + . 3 ;
 int D [N], SZ [N]; 
Vector < int > G [N]; 

int MID, SUM;
 int Check ( int NW, int F) 
{ 
    int l_mn = MID, r_mx = 0 , CNT = 0 ; 
    
    for(int i=0;i<sz[nw];i++)
    {
        int u=g[nw][i];
        if(u==f) continue;
        
        int len=check(u,nw)+1;
        if(len>mid) sum++;
        else if(len>(mid>>1)) cnt++,l_mn=min(l_mn,len);
        else if(len>0) r_mx=max(r_mx,len);
    }
    
    if(cnt)
    {
        sum+=cnt;
        if(l_mn+r_mx<=mid)
        {
            sum--;
            return l_mn;
        }
    }
    return r_mx;
}

int main()
{
    scanf("%d%d",&n,&cut);
    int u,v;
    for(int i=1;i<n;i++)
        scanf("%d%d",&u,&v),g[u].push_back(v),g[v].push_back(u);
    for(int i=1;i<=n;i++)
        sz[i]=G [I] .size ();
    
    if (Cut + . 1 > = n-) 
        the printf ( " 0 \ n- " );
     the else 
    { 
        int L = 0 , R & lt = n-, ANS = 0 ;
         the while (L <= R & lt) 
        { 
            MID = (L + R & lt) >> . 1 ; // > Cut this solution is very excellent, the solution becomes almost put, is to adjust the range to the right of ans 
            SUM = 0 , Check ( . 1 , 0 ); 
IF (SUM> Cut) + L = MID . 1 ; the else ANS = MID, R & lt mid- = . 1 ; // important to note that the wording of this place! ! ! ! . // find the knife is cut, how much is the smallest ans, // but it is likely the smallest ans this time, the smallest cut knife cut-1, // so the time of writing, is <= cut when the answer is updated, but not greater than } the printf ( " % D \ n- " , ANS); } return 0 ; }

2> faction

 

3> Emperor troubles

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11609217.html