http://poj.org/problem?id=1611
水题
题意:就是找一共有多少个人感染了,0是感染学生的编号。
#include <stdio.h> #include <string.h> #define maxn 30005 int m,n; int belg[ maxn ]; int Find(int x) { int _x=x,_b; while( _x != belg[ _x ] ) _x = belg[ _x ]; while( x != belg[ x ] ) { _b = belg[ x ]; belg[ x ] = _x; x = _b; } return _x; } void unio(int x,int y) { int root1 = Find(x); int root2 = Find(y); if( root1 != root2 ) belg[root2] = root1; } int main() { // freopen("in.txt","r",stdin); int a,b,c,ans; while(scanf("%d%d",&m,&n),m||n) { for( int i = 0 ; i <= m ; i++ ) belg[ i ] = i; for( int i = 0 ; i < n ; i++ ) { scanf("%d%d",&a,&b); for( int j = 0 ; j < a-1 ; j++ ) { scanf("%d",&c); unio(b,c); } } ans = 0; a = Find(0); for(int i = 0 ; i <= m ; i++ ) if(Find(i)==a) ans++; printf("%d\n",ans); } return 0; }
时间: 2024-11-08 06:10:43