poj 1611 并查集 按数目合并
来源:互联网 发布:ubuntu装锐捷修改文件 编辑:程序博客网 时间:2024/06/06 03:53
并查集有两种优化方法,路径压缩和按秩合并,这题我用的是按数目合并,由于是第一次写,所以难免有许多bug,好在很好调。
#include<iostream>using namespace std;int father[30010],n,num[30010];void inti(){int i;for(i=0;i<n;i++){father[i]=i;num[i]=1;}}int find(int a){if(father[a]!=a)father[a]=find(father[a]);return father[a];}int main(){int i,j,m,k,a,b,result;while(cin>>n>>m&&(m||n)){inti();for(i=0;i<m;i++){cin>>k>>a;for(j=1;j<k;j++){cin>>b;a=find(a); //要写在循环里面b=find(b);if(a!=b) //很重要,因为a==b,就不用管了。{if(num[a]>num[b]){father[b]=a;num[a]+=num[b];}else{father[a]=b;num[b]+=num[a];}}}}result=num[find(0)];cout<<result<<endl;}return 0;}
0 0
- poj 1611 并查集 按数目合并
- POJ 1611 (并查集,按秩合并)
- poj 1611 The Suspects(启发式式合并,并查集)
- POJ 1182 食物链 (并查集+集合有条件合并)
- 并查集-按秩合并
- 按秩合并并查集
- 并查集(按秩合并)
- 并查集的按秩合并
- 并查集 POJ 1611
- 【并查集】POJ-1611
- poj 1611(并查集)
- poj 1611 并查集
- poj 1611 并查集
- 【POJ 1611 并查集】
- poj 1611 并查集
- poj 1611(并查集)
- poj-1611 并查集
- POJ 1611 并查集
- Win7下 dev-c++用c语言访问mysql数据库环境配置
- VPV + NAT
- phoneGap插件 .
- 26_数组函数的调用
- Linux下搭建SVN全过程
- poj 1611 并查集 按数目合并
- 提高编程技能的11个建议
- 【PHP应用】一维数组构建树形数组,用于商品分类
- python 转换彩图为黑白图 ,去噪
- 27_命令行变元
- 计算机经典教材(计算机牛人的必由之路)(转)
- 28_函数返回值
- MyEclipse 5.5中安装SVN在线安装方法
- 29_函数的嵌套调用