並查集入門
一個社團的合併一次,最後統計和0是在相同集合的人
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 30000 + 10;
int p[maxn];
int a[maxn];
int tofind(int x){
if(x == p[x]) return x;
return p[x] = tofind(p[x]);
}
void join(int x, int y){
int px = tofind(x);
int py = tofind(y);
p[px] = p[y];
}
int main(){
int n, m, k;
while(scanf("%d%d", &n, &m), n + m){
for(int i = 0; i <= n; i++) p[i] = i;
for(int j = 0; j < m; j++){
scanf("%d", &k);
for(int i = 0; i < k; i++){
scanf("%d", &a[i]);
if(i){
if(tofind(a[i-1]) != tofind(a[i])){
join(a[i-1], a[i]);
}
}
}
}
int ff = tofind(0);
int ans = 1;
for(int i = 1; i <= n; i++){
if(tofind(i) == ff) ans++;
}
printf("%d\n", ans);
}
return 0;
}