并查集的基本操作
2014-05-12 15:28
183 查看
#include<iostream> typedef int T; T const &Find(T const &element,T *father) { if(father[element]<0) { return element; } else { return father[element]=Find(father[element],father); } } T const &Union(T const &element_a,T const &element_b,T *father) { if(father[element_a]<father[element_b]) { father[element_a]+=father[element_b]; return father[element_b]=element_a; } else { father[element_b]+=father[element_a]; return father[element_a]=element_b; } } int main() { using namespace std; int N,M; cin>>N>>M; int *father=new int[N+1]; for(int i=0;i<=N;++i) { father[i]=-1; } for(int i=0;i<M;++i) { int n,data; cin>>n>>data; data=Find(data,father); for(int j=1;j<n;++j) { int other_data; cin>>other_data; other_data=Find(other_data,father); if(other_data!=data) { data=Union(other_data,data,father); } } } int max=1; for(int i=1;i<=N;++i) { if(father[i]<0 && max<-father[i]) max=-father[i]; } cout<<max<<endl; delete[] father; }
相关文章推荐
- 并查集的一些基本概念以及基本操作(初始化,合并,查询等操作)
- 亲戚【并查集】(内附并查集基本操作)
- 普通并查集基本操作
- 并查集的基本操作
- HDU 1856 More is better (并查集基本操作)
- (使用树结构来支持并查集操作8.4.4)POJ 2524 Ubiquitous Religions(并查集的基本操作: 求有多少个相互独立的集合)
- Tomcat基本操作
- git 基本操作
- [绍棠] 使用NSFileHandle类对文件进行基本操作,IOS文件操作
- MyBatis - (一) 基本数据操作命令和简单映射
- JDBC操作数据库基本步骤
- SQLite数据库的基本操作
- 03-数据库和数据表的基本操作
- Objective-C中Selector基本概念和操作
- Datatable的基本操作
- HNCU1326:算法2-8~2-11:链表的基本操作
- MySQL数据库表的基本操作——修改表ALTER TABLE,删除表
- DOM4j读取XML基本操作及常见错误
- NPOI使用入门(一)【对Excel基本操作】
- c语言链表的基本操作2