#include <stdio.h>
int stack[300000];
int find(int x)
{
int r,k,j;
r=x;
while(r!=stack[r])
r=stack[r];
k=x;
while(k!=r)
{
j=stack[k];
stack[k]=r;
k=j;
}
return r;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
stack[fx]=fy;
}
int main()
{
int n,m,k,i,a,b,j,fx,fy;
while(scanf("%d %d",&n,&m))
{
if(n==0&&m==0)
break;
for(i=0; i<n; i++)
stack[i]=i;
for(i=0; i<m; i++)
{
scanf("%d",&k);
scanf("%d",&a);
for(j=0; j<k-1; j++)
{
scanf("%d",&b);
merge(a,b);
}
}
int c=1;
fx=find(0); //先找到编号为0的人的根节点
for(i=1; i<n; i++)
{
fy=find(i); //不能直接用stack[0]与stack[i]的关系判断它俩的根节点是否为一个,因为有的依然是父节点
if(fx==fy)
c++;
}
printf("%d\n",c);
}
}
The Suspects
最新推荐文章于 2020-10-12 15:35:17 发布