poj 1611.The Suspects
来源:互联网 发布:centos虚拟机怎么改ip 编辑:程序博客网 时间:2024/05/20 04:09
The Suspects
#include<iostream.h>const int MAX_N=3000;int n, m, k;int set[MAX_N];//初始化并查集,即将set中n个元素初始化为n个只有一个元素的集合.void Init();//查找元素x所在集合的树根并返回.int Root(int x);//合并树根分别为r1和r2的两个集合.返回合并后集合的树根.int Union(int r1, int r2);int main(){int x, r1, r2;int i, j;while(cin>>n>>m&&!(n==0&&m==0)){Init();for(i=0;i<m;i++){cin>>k;if(k>0){cin>>x;r1=Root(x);for(j=1;j<k;j++){cin>>x;r2=Root(x);r1=Union(r1,r2);}}}cout<<-set[Root(0)]<<endl;}return 0;} void Init(){int i=0;while(i<n){set[i++]=-1;}}int Root(int x){int r, i, j;r=x;while(set[r]>=0){r=set[r];}i=x;while(set[i]>=0){j=set[i];set[i]=r;i=j;}return r;}int Union(int r1, int r2){if(r1==r2){return r1;}else if(set[r1]<set[r2]){set[r1]+=set[r2];set[r2]=r1;return r1;}else{set[r2]+=set[r1];set[r1]=r2;return r2;}}
- poj 1611.The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ-1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 - The Suspects
- POJ 1611 The Suspects
- Poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 the suspects
- voip 视频 通信中的三种 丢包 恢复 技术
- Webservice soap wsdl区别之个人见解
- 全排列打印(二)
- C语言数据类型占字节数
- Linux--Shell编程-case,for,while,until
- poj 1611.The Suspects
- 链表(游标)
- 快速排序
- 双链表
- C++ 常用数学函数库
- 设计模式之间区别 (1) 桥接模式和策略模式
- 如何通俗的理解递归
- 栈(链表实现)
- 为什么中国的程序员技术偏低