并查集及其应用
2014-09-04 09:43
267 查看
并查集初步
PS:今天入手了一台1993年产的IBM Model M弹簧轴机械键盘,真好用呀真好用~ ^_^
并查集经常借助树形数据结构来实现。
设Father[i]表示元素i所属于的集合编号。初始化Father[x]=x;即每个节点都是单独的一棵树
并查集具有两项基本操作:
Find(i) :返回元素i所属于的集合的编号
Int Find(int x)
{
If (x!=Father[x])
Father[x]=Find(Father[x]) //此处用到了路径压缩优化
Return Father[x];
}
Union(x,y):合并元素x和元素y所在的集合
Void Union(int x,int y)
{
Int fx=Find(x);
Int fy=Find(y);
If (fx!=fy)
Father[fy]=fx;
}
来看一个简单的例题:
Eg1:亲戚(原题网上一抓一大把T^T)
赤裸裸的并查集,不再解释了~
View Code
PS:今天入手了一台1993年产的IBM Model M弹簧轴机械键盘,真好用呀真好用~ ^_^
并查集经常借助树形数据结构来实现。
设Father[i]表示元素i所属于的集合编号。初始化Father[x]=x;即每个节点都是单独的一棵树
并查集具有两项基本操作:
Find(i) :返回元素i所属于的集合的编号
Int Find(int x)
{
If (x!=Father[x])
Father[x]=Find(Father[x]) //此处用到了路径压缩优化
Return Father[x];
}
Union(x,y):合并元素x和元素y所在的集合
Void Union(int x,int y)
{
Int fx=Find(x);
Int fy=Find(y);
If (fx!=fy)
Father[fy]=fx;
}
来看一个简单的例题:
Eg1:亲戚(原题网上一抓一大把T^T)
赤裸裸的并查集,不再解释了~
#include <iostream> using namespace std; struct abc { int x,y,dat; }; struct abc e[1000]; int f[1000]; int i,j,k,ans,n,m,tx,ty,tmp; int find(int x) { if (x!=f[x]) f[x]=find(f[x]); return f[x]; } void iunion(int x,int y) { int fx=find(x); int fy=find(y); if (fx!=fy) f[fy]=fx; } void qsort(int l,int r) { int i,j,mid; struct abc t; i=l; j=r; mid=e[(l+r)/2].dat; do{ while (e[i].dat<mid) i++; while (e[j].dat>mid) j--; if (!(i>j)) { t=e[i]; e[i]=e[j]; e[j]=t; i++; j--; } }while (i<=j); if (l<j) qsort(l,j); if (i<r) qsort(i,r); } int main() { cin>>n>>m; for (i=1;i<=m;i++) { cin>>tx>>ty>>tmp; e[i].x=tx; e[i].y=ty; e[i].dat=tmp; } for (i=1;i<=n;i++) f[i]=i; qsort(1,m); k=1; ans=0; for (i=1;i<=n-1;i++) { while (find(e[k].x)==find(e[k].y)) k++; iunion(e[k].x,e[k].y); ans=ans+e[k].dat; cout<<e[k].x<<" "<<e[k].y<<" "<<e[k].dat<<endl; } cout<<ans<<endl; }
View Code
相关文章推荐
- 并查集及其应用
- 并查集及其应用
- 并查集及其在最小生成树中的应用
- 并查集 (Union-Find Sets)及其应用
- (算法)并查集及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集及其应用
- 并查集及其应用
- 并查集(Union-Find Sets)及其应用
- 并查集及其简单应用
- 并查集及其在最小生成树中的应用
- 并查集 (Union-Find Sets)及其应用
- 并查集实现及其应用
- 并查集及其应用——kruscal法求最小生成树
- 并查集及其应用
- 并查集及其简单应用:优化kruskal算法
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用