#include <iostream>
#include <cstdio>
#include <cstring>
#define maxsize 30010
using namespace std;
typedef struct tree1{
int pre;
int num;
}Tree;
Tree tree[maxsize];
int find(int x){
if(tree[x].pre == x){
return x;
}
return (tree[x].pre = find(tree[x].pre));
}
void merge(int x, int y){
int f1 = find(x);
int f2 = find(y);
if(f1!=f2){
tree[f1].num += tree[f2].num;
tree[f2].pre = f1;
}
}
int main()
{
int m, n, i, j, k;
while(scanf("%d%d",&n, &m)!=EOF){
if(0==n&&0==m){
break;
}
for(i = 0; i < n; i++){
tree[i].pre = i;
tree[i].num = 1;
}
while(m--){
scanf("%d", &k);
int a, b;
scanf("%d", &a);
for(i = 1; i < k; i++){
scanf("%d", &b);
merge(a,b);
}
}
int t = find(0);
printf("%d\n", tree[t].num);
}
return 0;
}
/*
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
*/
http://acm.hrbeu.edu.cn/index.php?act=problem&id=1003&cid=50
感染者(并查集入门)
最新推荐文章于 2021-12-26 22:25:06 发布