#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<algorithm>#include<stack>#include<queue>#include<map>#include<vector>#include<iostream>using namespace std;const int maxn = 30000+10;const int maxm = 500+10;int n,m;int root[maxn];//root[i]表示i的祖先是root[i]//int group[maxn];//group[i]表示i集合的人数//找x的祖先int Find( int x){ //printf("$%d %d$/n",x,root[x]); if( x == root[x]) return root[x];//祖先是自己 return root[x] = Find(root[x]);}//合并x yvoid Union( int x, int y){ int px = Find(x);//找x的祖先 int py = Find(y);//找y的祖先 if( px == py)//x和y的祖先是同一个人 return; root[px] = py;//px的祖先为py}int main(){ while( ~scanf("%d%d",&n,&m) && n) { for( int i = 0; i < n; i++) { //初始化 i的祖先还是i 每个人开始是独立的 每组一个人 root[i] = i; //group[i] = 1; } for( int i = 0; i < m; i++) { int k; scanf("%d",&k); if( !k) continue; int x,y; scanf("%d",&x); for( int j = 1; j < k; j++) { scanf("%d",&y); Union( x,y); } } int tmp = Find(0);//找到0的祖先 int ans = 1; for( int i = 1; i < n; i++) if( Find(i) == tmp) ans++; printf("%d/n",ans); } return 0;}
新闻热点
疑难解答