gavanwanggw

导航

2015 Multi-University Training Contest 3--1011 Work

代码:
#include<cstdio>
#include<cstring>
using namespace std;

int n,k;
int father[105],son[105];
int dd;

void Find(int a)
{
    //int r=son[a];
    while(father[a]!=a)
    {
        a=father[a];
        son[a]+=(dd);
        //r=son[a];
    }
    //father[r]=a;
    //return a;
}

void Union(int a,int b)
{
    father[b]=a;
    son[a]+=(son[b]+1);
    dd=son[b]+1;
    Find(a);
}

int main()
{
    while(scanf("%d%d",&n,&k)==2)
    {
        for(int i=1; i<=n; i++)
        {
            father[i]=i;
            son[i]=0;
        }
        for(int i=1; i<n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            Union(a,b);

        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            //printf("%d %d\n",i,son[i]);
            if(son[i]==k)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

posted on 2017-05-05 11:10  gavanwanggw  阅读(106)  评论(0编辑  收藏  举报