pku 1611 The Suspects

#include <stdio.h>
#include <string.h>
#define MAXN 30001
int father[MAXN];
inline int find(int x)
{
    int i,t;
    for(i=x; father[i]>=0; i=father[i]) ;
    while(x!=i)
    {
        t=father[x];
        father[x]=i;
        x=t;
    }
    return i;
}
inline void merge(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx==fy) return;
    if(father[fx]>father[fy])
    {
        father[fy]+=father[fx];
        father[fx]=fy;
    }
    else
    {
        father[fx]+=father[fy];
        father[fy]=fx;
    }
}
int main()
{
    int n,m,i,j,k;
    while(scanf("%d %d",&n,&m),n+m)
    {
        memset(father,-1,sizeof(*father)*(n+1));
        while(m--)
        {
            scanf("%d",&k);
            scanf("%d",&i);
            j=i;
            k--;
            while(k--)
            {
                scanf("%d",&j);
                merge(i,j);
            }
        }
        int cnt=1,f0=find(0);
        for(int i=1; i<n; i++)
            if(find(i)==f0) cnt++;
        printf("%d\n",cnt);
    }
    return 0;
}

posted @ 2010-08-25 19:25  菜到不得鸟  阅读(89)  评论(0)    收藏  举报