zoj 1789 The Suspects
2011-08-19 10:11
337 查看
这道题是并查集的题,写过这道题之后,我明白了一点,就是从根上改变所属father域
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int p[30000+100]; int a[30000+100]; int find(int x) { if(p[x] == x) return x; p[x] = find(p[x]); return p[x]; } int main() { int n,m,k,i,count,temp; while(scanf("%d%d",&n,&m) == 2,n*n+m*m) { for(i=0;i<n;i++) p[i] = i; while(m--) { scanf("%d",&k); for(i=0;i<k;i++) scanf("%d",&a[i]); for(i=0;i<k-1;i++) if(find(p[a[i]]) != find(p[a[i+1]])) p[find(p[a[i+1]])] = find(p[a[0]]); } count = 0; for(i=0;i<n;i++) if(find(i) == find(0)) count++; printf("%d\n",count); } return 0; }
相关文章推荐
- zoj 1789||poj 1611 The Suspects(并查集,简单)
- zoj 1789 The Suspects (并查集)
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- zoj 1789 || poj 1611 The Suspects(并查集)
- zoj 1789
- ZOJ 1789 The Suspects
- ACM zoj 1789(并查集实现)
- zoj - 1789 - The Suspects
- zoj 1789
- 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简单并查集