zoj 1789 The Suspects
2012-02-04 15:34
369 查看
/* zoj_1789 并查集 并查集的简单应用,可以继续优化。 */ #include <iostream> #include <cstdio> #include <vector> #include <string.h> #define N 30002 using namespace std; int father ,num ,h ; void init( int n ) { int i; for( i=0;i<n;i++ ) father[i]=i , h[i]=0 ; } int find_set( int a ) { if( a!=father[a] ) { father[a]=find_set(father[a]); } return father[a]; } void union_set( int a,int b ) { if( a==b ) return; if( h[a]>h[b] ) father[b]=a; else { if( h[a]==h[b] ) h[b]++; father[a]=b; } } int main() { int n,m,k,i,j,co; while( scanf( "%d%d",&n,&m ) && (n || m) ) { init(n); while( m-- ) { scanf( "%d",&k ); for( i=0;i<k;i++ ) scanf( "%d",&num[i] ); for( i=0;i<k;i++ ) for( j=i+1;j<k;j++ ) union_set( find_set( num[i] ),find_set( num[j] ) ); } co=0; for( i=0;i<n;i++ ) find_set(i); for( i=0;i<n;i++ ) if( father[i]==father[0] ) co++; printf( "%d\n",co ); } return 0; }
相关文章推荐
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- zoj 1789 || poj 1611 The Suspects(并查集)
- zoj 1789||poj 1611 The Suspects(并查集,简单)
- zoj 1789 The Suspects (并查集)
- ZOJ 1789
- zoj 2158 poj 1789 Truck History (Prim)
- zoj 1789 判汉密顿图
- ZOJ 1789 水水的并查集,刚开始学会并查集,做了此题。经过一次失败后终于AC了哈哈~ AC感觉就是好啊!与大家共分享啊
- zoj 1789 The Suspects
- zoj1789(并查集)
- zoj 1789 The Suspects
- ZOJ1789(The Suspects)j简单并查集
- ZOJ 2158/POJ 1789 - Truck History
- zoj1789-The Suspects
- ZOJ-1789
- ZOJ 2158 && POJ 1789 Truck History (经典MST)
- ZOJ 1789 The Suspects
- zoj-1789