【数据结构】并查集

介绍

在这里插入图片描述

  1. 数组的下标对应集合中元素的编号
  2. 数组中如果为负数,负号代表根,数字代表该集合中元素个数
  3. 数组中如果为非负数,代表该元素双亲在数组中的下标

解决的问题

  1. 查找元素属于哪个集合
    沿着数组表示树形关系以上一直找到根(即:树中中元素为负数的位置)
  2. 查看两个元素是否属于同一个集合
    沿着数组表示的树形关系往上一直找到树的根,如果根相同表明在同一个集合,否则不在
  3. 将两个集合归并成一个集合
    将两个集合中的元素合并
    将一个集合名称改成另一个集合的名称
  4. 集合的个数
    遍历数组,数组中元素为负数的个数即为集合的个数

并查集的实现

class UnionFindSet
{
public:
	UnionFindSet(size_t n)
		:_ufs(n,-1)
	{}

	//合并两个不同的集合
	void Union(int x1,int x2)
	{
		int root1 = FindRoot(x1);
		int root2 = FindRoot(x2);

		if (root1 == root2)
		{
			return;
		}
		if (root1 > root2)
		{
			swap(root1, root2);
		}
		//把x2合并到x1
		_ufs[root1] += _ufs[root2];
		_ufs[root2] = root1;
	}

	int FindRoot(int x)const
	{
		int root = x;
		while (_ufs[root]>=0)
		{
			root = _ufs[root];
		}
		return root;
	}

	//判断是否在同一个集合
	bool IsInSet(int x1,int x2) const
	{
		return FindRoot(x1) == FindRoot(x2);
	}

	//判断有几个集合
	size_t Set_Number() const
	{
		size_t size = 0;
		for (size_t i = 0; i < _ufs.size(); i++)
		{
			if (_ufs[i] < 0)
				++size;
		}
		return size;
	}
private:
	vector<int> _ufs;
};

并查集的应用

547.省份数量

class Solution {
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        vector<int> ufs(isConnected.size(),-1);

        auto FindRoot=[&ufs](int x)
        {
            while(ufs[x]>=0)
            {
                x=ufs[x];
            }
            return x;
        };

        for(size_t i=0;i<isConnected.size();i++)
        {
            for(size_t j=0;j<isConnected[i].size();j++)
            {
                if(isConnected[i][j]==1)
                {
                    int root1=FindRoot(i);
                    int root2=FindRoot(j);
                    if(root1!=root2)
                    {
                        ufs[root1]+=ufs[root2];
                        ufs[root2]=root1;
                    }
                }
            }
        }

        int n=0;
        for(auto e:ufs)
        {
            if(e<0)
                ++n;
        }
        return n;
    }
};

990等式方程的可满足性

class Solution {
public:
    bool equationsPossible(vector<string>& equations) {
        vector<int> ufs(26,-1);
        auto findRoot=[&ufs](int x)
        {
            while(ufs[x]>=0)
                x=ufs[x];
            return x;
        };

        //第一遍先把相等的加到一个集合中
        for(auto&str:equations)
        {
            if(str[1]=='=')
            {
                int root1=findRoot(str[0]-'a');
                int root2=findRoot(str[3]-'a');
                if(root1!=root2)
                {
                    ufs[root1]+=ufs[root2];
                    ufs[root2]=root1;
                }
            }
        }

        //第二遍找不相等的在不在一个集合,如果在就返回false,与集合相悖
        for(auto& str:equations)
        {
            if(str[1]=='!')
            {
                int root1=findRoot(str[0]-'a');
                int root2=findRoot(str[3]-'a');
                if(root1==root2)
                    return false;
            }
        }

        return true;
    }
};
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值