并查集(按秩合并,路径压缩)

    并:Union:合并:合并两个不同的集合;
    查:Find:查找:判断两个集合是否在一个集合;
    集:Set:集合;

注:下面所有的代码中 双亲孩子表示法中数组 S 的初值应该赋为 -1;

/**
    并:Union:合并:合并两个不同的集合;
    查:Find:查找:判断两个集合是否在一个集合;
    集:Set:集合;
*/

/**
#include <iostream>

using namespace std;

#define MAXN 1000
typedef int ElementType;
typedef int SetName;
typedef ElementType SetType[MAXN];

SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合

int main()
{
    int n;
    cin >> n;

    return 0;
}

SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
{
    while(S[x]>=0)
        x=S[x];
    return x;
}

void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
{
    root1=FindFather(S,root1);
    root2=FindFather(S,root2);
    if(root1!=root2)
        S[root1]=root2;
}
*/

 2)按秩合并集合,对Union进行优化;

/**
    2)按秩合并集合,对Union进行优化;
*/

/**
    并:Union:合并:合并两个不同的集合;
    查:Find:查找:判断两个集合是否在一个集合;
    集:Set:集合;
*/

/**
#include <iostream>

using namespace std;

#define MAXN 1000
typedef int ElementType;
typedef int SetName;
typedef ElementType SetType[MAXN];

SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合

int main()
{
    int n;
    cin >> n;

    return 0;
}

SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
{
    while(S[x]>=0)
        x=S[x];
    return x;
}

void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
{
    root1=FindFather(S,root1);
    root2=FindFather(S,root2);
    if(root1!=root2)
    {
        if(S[root1]<S[root2]) //如果集合root1的元素要多一点
        {
            S[root1]+=S[root2];
            S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
        }
        else if(S[root2]<S[root1])
        {
            S[root2]+=S[root1];
            S[root1]=root2;
        }
    }
}
*/

3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
    以便为后面的Find函数减低查找时间;对Find进行优化;


/**
    3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
    以便为后面的Find函数减低查找时间;对Find进行优化;
*/

/**
    并:Union:合并:合并两个不同的集合;
    查:Find:查找:判断两个集合是否在一个集合;
    集:Set:集合;
*/


#include <iostream>

using namespace std;

#define MAXN 1000
typedef int ElementType;
typedef int SetName;
typedef ElementType SetType[MAXN];

SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合

int main()
{
    int n;
    cin >> n;

    return 0;
}

SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
{
    if(S[x]<0)
        return x;
    else
        return S[x]=Find(S,S[x]);
}

void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
{
    root1=FindFather(S,root1);
    root2=FindFather(S,root2);
    if(root1!=root2)
    {
        if(S[root1]<S[root2]) //如果集合root1的元素要多一点
        {
            S[root1]+=S[root2];
            S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
        }
        else if(S[root2]<S[root1])
        {
            S[root2]+=S[root1];
            S[root1]=root2;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值