hdu1232并查集
来源:互联网 发布:安卓埋雷软件破解版 编辑:程序博客网 时间:2024/04/28 23:52
int find(int x){ if(F[x]==x)return x; else return F[x]=find(F[x]);}void unions(int x,int y) { F[x]=y; }
路径压缩:每次查找的时候,如果路径较长,则修改信息,以便下次查找的时候速度更快
步骤:1、找到根点
2、修改查找路径上的所有节点,将他们都指向根结点
#include <iostream>#include <stdio.h>#include <memory.h>const int maxn=1010;using namespace std;int F[maxn];int N,M;void init(){ for(int i=1;i<=N;i++)F[i]=i;}int Find(int x){ int r=x,tmp; while(F[x]!=x)x=F[x]; while(r!=x){ tmp=F[r]; F[r]=x;//路径上的节点都连在根节点上 r=tmp; } return x;}void Unit(int u,int v){ int t1=Find(u); int t2=Find(v); if(t1!=t2) F[t1]=t2;}int main(){ //freopen("in.txt","r",stdin); int a,b,ans; while(scanf("%d",&N)!=EOF&&N){ ans=0; scanf("%d",&M); init(); for(int i=0;i<M;i++){ scanf("%d %d",&a,&b); Unit(a,b); } for(int i=1;i<=N;i++){ if(F[i]==i)ans++; } printf("%d\n",ans-1); } return 0;}
0 0
- hdu1232 并查集
- hdu1232 并查集
- HDU1232并查集
- hdu1232并查集
- 并查集-HDU1232
- 并查集hdu1232
- hdu1232 并查集
- hdu1232(并查集)
- HDU1232并查集
- 并查集-hdu1232
- 并查集-hdu1232
- hdu1232并查集
- hdu1232(并查集)
- hdu1232(并查集)
- hdu1232并查集
- hdu1232 并查集
- HDU1232 并查集<并>
- HDU1232(并查集)
- 第2章 注重实效的途径
- linux
- js-匀速/缓动动画-简单封装
- JDK安装教程
- Unity AssetStore 下载文件目录
- hdu1232并查集
- Spring基础:快速入门spring(2):理解Ioc
- inode 连接vpn报错"获取虚拟网卡失败 请检查网卡驱动 inode vpn virtual nic 是否被禁“解决办法
- 基于Sobel和Canny边缘检测
- RX学习笔记之map变换
- mapreduce之mapper、reducer个数
- linux常用命令的英文单词缩写
- 进程调度和内核数据结构
- sublime text3配置方法、插件推荐及使用技巧