查找某个并查集里元素的个数
#include <iostream>
#include <stdio.h>
using namespace std;
#define MMAX 30005
int p[MMAX];
void init() {
for(int i = 0; i < MMAX; i++) {
p[i] = -1;
}
}
int find(int x) {
int s;
for(s = x; p[s] >= 0; s = p[s]);
while(s != x) {
int tmp = p[x];
p[x] = s;
x = tmp;
}
return s;
}
int unionSet(int a, int b) {
int t1 = find(a);
int t2 = find(b);
int tmp = p[t1]+p[t2];
if(p[t1] > p[t2]) {
p[t1] = t2;
p[t2] = tmp;
}
else {
p[t2] = t1;
p[t1] = tmp;
}
}
int main() {
int n, m;
while(scanf("%d%d", &n, &m), !(0 == n && 0 == m)) {
init();
for(int i = 0; i < m; i++) {
int k;
int a, b;
scanf("%d", &k);
scanf("%d", &a);
for(int j = 0; j < k-1; j++) {
scanf("%d", &b);
int t1 = find(a);
int t2 = find(b);
if(t1 != t2) {
unionSet(a, b);
}
}
}
printf("%d\n", -p[find(0)]);
}
return 0;
}