Hud 1856 More is Better[并查集]
来源:互联网 发布:软件流程图实例 编辑:程序博客网 时间:2024/06/07 00:59
/* Hud 1856 More is Better 并查集. rank数组表示,该树或者该集合成员个数. 把元素少的并到元素多的集合里面.*/#include<cstdio>#define MAX 10000000int father[MAX+2],rank[MAX+2];void Init(){ for(int i=1;i<=MAX;i++) { father[i]=i; rank[i]=1; }}int Find_Father(int x){ if(father[x]!=x) father[x]=Find_Father(father[x]); return father[x];}void Union(int a,int b){ int x=Find_Father(a); int y=Find_Father(b); if(x==y) return; if(rank[x]>rank[y]) { father[y]=x; rank[x]+=rank[y]; } else { father[x]=y; rank[y]+=rank[x]; }}int main(){ int n; while(~scanf("%d",&n)) { Init(); int a,b; for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b); Union(a,b); } int max=0; for(int i=1;i<=MAX;i++) if(rank[i]>max) max=rank[Find_Father(i)]; printf("%d\n",max); }}/* 小小模板.*/#define MAX ?int father[MAX],rank[MAX];void Init()//初始化.每个结点都是一个集合{ for(int i=1;i<=MAX;i++) { father[i]=i; rank[i]=1; }}int Find_Father(int x){//递归路径压缩 if(x==Find_Father(x)) father[x]=Find_Father(x); return father[x];}void Union(int a,int b){ int x=Find_Father(a); int y=Find_Father(b); if(x==y) return; if(rank[x]>rank[y]) { father[y]=x; rank[x]+=rank[y]; } else { father[x]=y; rank[y]+=rank[x]; }}
相当囧啊!!
写了个小小的模板竟然错了。该模板做废。
- Hud 1856 More is Better[并查集]
- More is better hdu1856 并查集
- hdu1856 ,More is better,并查集
- 并查集C - More is better
- HDOJ1856 More is better 【并查集】
- Hdu1856 - More is better - 并查集
- HDU1856 More is better 并查集
- More is better(并查集)
- 并查集 HDOJ1856 More is better
- HDU1856 More is better 并查集
- More is better---并查集实践
- More is better(并查集)
- hdu1856 More is better 并查集
- HDU1856 More is better(并查集)
- hdu1856 More is better 并查集
- more is better(并查集)
- hdu1856 More is better--并查集
- hdu1856 More is better 并查集
- UNIX环境高级编程——线程同步之互斥锁、读写锁和条件变量(小结)
- 解决listview中出现重复的项
- 为什么每次打开Myeclipse都要重新配置Tomcat
- 按键配置文件位置
- 数据挖掘的常见方法
- Hud 1856 More is Better[并查集]
- 取扩展名
- Template模版实例(C++)
- 指向指针的指针
- 线性表操作
- tsm 只读模式修改为读写
- 汇编语言:两数求和
- Visual Studio 2013预览版中的 C++11 新特性
- 从键盘输入两个十以内的数求和,并把和用十进制在显示器上将显示