首页 > 代码库 > HDU 5326

HDU 5326

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int vis[200];
struct Point
{
    int sum;
    int last;
}s[200];

int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        if(n==1&&m==0)//注意特判!!!
        {
            printf("1\n");
            continue;
        }
        for(int i=0;i<=104;i++)
            s[i].sum=0,s[i].last=0,vis[i]=0;
        int x,y;
        if(m>=n)
        {
            printf("0\n");
            continue;
        }
        for(int i=0;i<n-1;i++)
        {
            scanf("%d %d",&x,&y);
            s[y].last=x;
            int ss=y;
            if(!vis[y])
            {
                while(1)
                {
                  if(s[ss].last!=0)
                    {
                        s[s[ss].last].sum++;
                       ss=s[ss].last;
                    }
                 else
                    break;
                }
            }
            else
            {
                int d=s[ss].sum+1;
                while(1)
                  {
                 if(s[ss].last!=0)
                  {
                     s[s[ss].last].sum+=d;
                     ss=s[ss].last;
                   }
                 else
                     break;
                  }
            }
           ss=s[ss].last;
            while(1)
            {
                if(s[ss].last!=0)
                {
                    s[s[ss].last].sum++;
                    ss=s[ss].last;
                }
                else
                    break;
            }
            vis[x]=vis[y]=1;
        }
  //      printf("%d\n",s[1].sum);
        int ans=0;
        for(int i=1;i<=100;i++)
            if(m==s[i].sum&&vis[i])
                ans++;
        printf("%d\n",ans);
    }
}

 

HDU 5326