zoj 1789 The Suspects
2012-11-14 02:12
302 查看
并查集
把所有的联通起来,然后查找0所在的圈子包含的数量就可以了
把所有的联通起来,然后查找0所在的圈子包含的数量就可以了
#include <iostream> #include <cstdio> using namespace std; #define N 30010 int parent ; int treeId ; inline void init(int i) { treeId[i] = -1; } int find(int x) { if (treeId[x]>=0) { return treeId[x]=find(treeId[x]); } return x; } int main() { int n,m; while (scanf("%d %d",&n,&m)==2) { if (n==0&&m==0) break; for (int i=0;i<n;i++) { init(i); } while(m--) { int nm; scanf("%d",&nm); int father; int fatherParent =0; if(nm>0) { scanf("%d",&father); nm--; fatherParent= find(father); } while(nm--) { int tmp; scanf("%d",&tmp); int tmpParent = find(tmp); if(tmpParent!=fatherParent) { treeId[fatherParent] += treeId[tmpParent]; treeId[tmpParent] = fatherParent; } } } printf("%d\n",-treeId[find(0)]); } 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 - The Suspects
- zoj 1789
- 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简单并查集