#include <iostream>
#include 
<cstdio>

const int maxn = 30000 + 5;

using namespace std;

int father[maxn],rank[maxn];

void init( int n )
{
    
for ( int i = 0; i < n; i++)
    {
        father[i] 
= i;
        rank[i] 
= 1;
    }
}

int findSet( int n )
{
    
if(father[n] != n)
        father[n] 
= findSet(father[n]);
    
return father[n];
}

void Union( int a, int b )
{
    
int x = findSet( a );
    
int y = findSet( b );

    
if( x == y ) return ;
    
if( rank[x] >= rank[y] )
    {
        father[y] 
= x;
        rank[x] 
+= rank[y];
    }
    
else
    {
        father[x] 
= y;
        rank[y] 
+= rank[x];
    }
}

int main()
{
    
int m, n, count, temp, first;
    
while~scanf("%d%d"&n, &m ) && n )
    {
        init(n);
        
while( m-- )
        {
            scanf(
"%d%d"&count, &first );
            
forint i = 1; i < count ;i ++)
            {
                scanf(
"%d"&temp);
                Union( first, temp );
            }
        }
        printf(
"%d\n",rank[findSet(0)]);
    }
    
return 0;
}
posted on 2010-07-29 07:09 Vontroy 阅读(222) 评论(0)  编辑 收藏 引用 所属分类: 并查集POJ

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理