boolunit(int x, int y){ x = find(x); y = find(y); if (x == y)//同一集合 returnfalse; if (Rank[x] > Rank[y]) swap(x, y);//避免大的挂在小的名下 par[x] = y; Rank[x] += Rank[y]; Rank[y] = Rank[x]; returntrue; }
intmain(){ int n, m; while (scanf_s("%d%d", &n, &m) != EOF) { if (n == 0 && m == 0) break; initial(n); for (int i = 0; i < m; i++) { int num; scanf_s("%d", &num); int ori; scanf_s("%d", &ori); for (int j = 1; j < num; j++) { int a; scanf_s("%d", &a); unit(ori, a); } } int res = Rank[find(0)]; printf("%d\n", res); }