Poj 1611The Suspects

来源:互联网 发布:iqr淘宝网首页 编辑:程序博客网 时间:2024/06/03 18:15
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 30005
int father[N],count[N];
void makeSet(int x)
{
    father[x]=x;
    count[x]=1;
}
int findRoot(int x)
{
    if(x!=father[x])
        x=findRoot(father[x]);
    return x;
}
void unionSet(int a,int b)
{
    a=findRoot(a);
    b=findRoot(b);
    if(a==b)
        return ;
    if(count[a]<=count[b])
    {
        father[a]=b;
        count[b]+=count[a];
    }else
    {
        father[b]=a;
        count[a]+=count[b];
    }
}
int main(int argc,char *argv[])
{
    freopen("input1.txt","r",stdin);
    int n,m,t,a,b;
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        for(int i=0;i<n;i++)
            makeSet(i);
        while(m--)
        {
            scanf("%d%d",&t,&a);
            t--;
            while(t--)
            {
                scanf("%d",&b);
                unionSet(a,b);
            }
        }
        printf("%d\n",count[findRoot(0)]);
    }
    return 0;
}

0 0