您的位置:首页 > 其它

并查集及其应用

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)

赤裸裸的并查集,不再解释了~

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: