#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
using namespace std;
#define MAXN 30000 + 11
int fa[MAXN];
int num[MAXN];
int findfa(int x)
{
if(fa[x] == -1)
return x;
return fa[x] = findfa(fa[x]);
}
void unit( int x, int y)
{
int fax = findfa(x);
int fay = findfa(y);
if(fax != fay)
{
fa[fax] = fay;
num[fay] += num[fax];
}
}
int main()
{
int n, m, a, b, c;
while(scanf("%d %d",&n, &m)!= EOF && (n + m))
{
memset(fa, -1, sizeof(fa));
for(int i = 0; i <= n; i++)
num[i] = 1;
for(int i = 1; i <= m; i++)
{
scanf("%d",&a);
scanf("%d",&c);
for(int i = 1; i < a; i++)
{
scanf("%d",&b);
unit(b, c);
//cout<<fa[b]<<endl;
}
}
int x = findfa(0);
printf("%d\n",num[x]);
}
return 0;
}
The Suspects 并差集
最新推荐文章于 2024-06-14 12:31:25 发布