Posted on 2010-08-26 20:51
lzh525 阅读(388)
评论(0) 编辑 收藏 引用
1
#include<stdio.h>
2
int p[30001];
3
int h[30001];
4
5
void Init(int n)
6

{
7
for(int i=0;i<n;i++)
8
{
9
p[i]=i;
10
h[i]=0;
11
}
12
}
13
14
int find(int x)
15

{
16
if(x!=p[x])
17
p[x]=find(p[x]);
18
return p[x];
19
}
20
21
void connect(int a,int b)
22

{
23
int x,y;
24
x=find(a);
25
y=find(b);
26
if(h[x]>=h[y])
27
{
28
p[y]=x;
29
h[x]++;
30
}
31
else
32
{
33
p[x]=y;
34
h[y]++;
35
}
36
}
37
38
int main()
39

{
40
int k,a,i,m,n,num,t,first;
41
while(scanf("%d%d",&m,&n))
42
{
43
if(m==0&&n==0)
44
return 0;
45
Init(m);
46
num=1;
47
while(n--)
48
{
49
scanf("%d",&k);
50
first=0;
51
for(i=1;i<=k;i++)
52
{
53
scanf("%d",&a);
54
if(first)
55
connect(a,t);
56
first=1;
57
t=a;
58
}
59
}
60
for(i=1;i<m;i++)
61
if(find(0)==find(i))
62
num++;
63
printf("%d\n",num);
64
}
65
return 0;
66
}