计蒜客 T2237 魔法 分类讨论

Code: 

#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 200005 
using namespace std; 
int du[maxn],from[maxn],to[maxn];       
int main() 
{
    // setIO("input"); 
    int n,k;  
    scanf("%d%d",&n,&k); 
    for(int i=1;i<n;++i) 
    {
        int x,y; 
        scanf("%d%d",&x,&y); 
        ++du[x],++du[y];  
        from[i]=x, to[i]=y; 
    }
    int ans=0, cnt=0; 
    for(int i=1;i<=n;++i) if(du[i]==1) ++ans; 
    if(k==0) printf("%d\n",ans);     
    else 
    {
        cnt=ans; 
        for(int i=1;i<n;++i) 
        {
            int u=from[i], v=to[i]; 
            int x=min(du[u], du[v]); 
            int y=max(du[u], du[v]);  
            if(x<=2&&y>=3) ans=min(ans, cnt-1); 
            if(x>=3) ans=min(ans, cnt-2); 
        }         
        printf("%d\n",ans); 
    }
    return 0; 
}

  

posted @ 2019-07-26 11:09  EM-LGH  阅读(134)  评论(0编辑  收藏  举报