#include <iostream>
using namespace std;
/*
思路:并查集读取并分组 找到0所在组的人数
*/
int n, m;
int students[30002];
void init() {
for (int i = 0; i < n; i++) {
students[i] = i;
}
}
int find_set(int x) {
if (students[x] == x)
return x;
else {
students[x] = find_set(students[x]);
}
}
void Union_set(int x, int y) {
x = find_set(x);
y = find_set(y);
students[x] = y;
}
int sum() {
int res = 0;
int root = find_set(0);//找到0节点的根元素
for (int i = 0; i < n; i++) {
if (find_set(i) == root)
res++;
}
return res;
}
int main() {
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
init();
while (m--) {
int count,root,s;
cin >> count;
cin >> root;//作为根节点剩下的同学都跟他合并
count--;
while (count--) {
cin >> s;
Union_set(root, s);
}
}
cout << sum() << endl;
}
return 0;
}
Poj-1611
最新推荐文章于 2024-09-13 15:56:13 发布