【高阶数据结构】并查集

并查集


并查集

1、概念

并查集(Union-Find)是一种可以用来判断同属一个集合中相互关联的元素属于几个集合,也可以用来判断图结构中的两点是否是连通, 它也是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。

2、根据人找编号 / 根据编号找人(简单介绍一下并查集)

(1)代码展示

cpp 复制代码
// UnionFindSet.h
#pragma once

#include <vector>
#include <map>

template <class T>
class UnionFindSet
{
private:
	std::vector<T> _a;			// 编号找人
	std::map<T, int> _indexmap; // 人找编号的映射关系
public:
	UnionFindSet(const T* a, size_t n)
	{
		for (size_t i = 0; i < n; i++)
		{
			_a.push_back(a[i]);  // 将传进来的值存入到vector中
			_indexmap[a[i]] = i; // 映射关系
		}
	}
};
cpp 复制代码
// photo.cpp
#include <iostream>
#include "UnionFindSet.h"

int main()
{
	std::string s[] = { "张三", "李四", "王五", "赵六" };
	UnionFindSet<std::string> ufs(s, 4);
	return 0;
}

(2)调试结果

(3)优化1:小的往大的合并

(4)优化2:压缩路径

cpp 复制代码
	// 找根
	int FindRoot(int x)
	{
		int root = x;
		while (_ufs[root] >= 0)
		{
			root = _ufs[root]; // 下标和值的关系
		}

		// 压缩路径
		while (_ufs[x] >= 0)
		{
			int parent = _ufs[x]; // 提前存一下父亲结点的值
			_ufs[x] = root; // 往上找
			x = parent;
		}
		return root;
	}

3、并查集操作和演示题目

(1)并查集操作

i、思路


ii、总体代码
cpp 复制代码
class UnionFindSet
{
private:
	std::vector<int> _ufs;
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;

		// 更新
		_ufs[root1] += _ufs[root2]; // 前面的值+=后面的值
		_ufs[root2] = root1; // 更新后面的值为前面的值(双亲根)
	}
	// 找根
	int FindRoot(int x)
	{
		int parent = x;
		while (_ufs[parent] >= 0)
		{
			parent = _ufs[parent]; // 下标和值的关系
		}
		return parent;
	}
	// 判断是否是同一个树
	bool IsSet(int x1, int x2)
	{
		return FindRoot(x1) == FindRoot(x2);
	}
	// 算树的数量
	int Size()
	{
		int n = _ufs.size();
		int size = 0;
		for (int i = 0; i < n; i++)
		{
			if (_ufs[i] < 0)
			{
				size++;
			}
		}
		return size;
	}
};

(2)演示题目:省份数量

i、做法一:自己写一个并查集

leetcode题目链接跳转

cpp 复制代码
class UnionFindSet
{
private:
	std::vector<int> _ufs;
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;

		// 更新
		_ufs[root1] += _ufs[root2]; // 前面的值+=后面的值
		_ufs[root2] = root1; // 更新后面的值为前面的值(双亲根)
	}
	// 找根
	int FindRoot(int x)
	{
		int parent = x;
		while (_ufs[parent] >= 0)
		{
			parent = _ufs[parent]; // 下标和值的关系
		}
		return parent;
	}
	// 判断是否是同一个树
	bool IsSet(int x1, int x2)
	{
		return FindRoot(x1) == FindRoot(x2);
	}
	// 算树的数量
	int Size()
	{
		int n = _ufs.size();
		int size = 0;
		for (int i = 0; i < n; i++)
		{
			if (_ufs[i] < 0)
			{
				size++;
			}
		}
		return size;
	}
};
class Solution 
{
public:
    int findCircleNum(vector<vector<int>>& isConnected) 
    {
        UnionFindSet ufs(isConnected.size());
        for (int i = 0; i < isConnected.size(); i++)
        {
            for (int j = 0; j < isConnected[i].size(); j++)
            {
                if (isConnected[i][j] == 1)
                {
                    ufs.Union(i, j);
                }
            }
        }
        return ufs.Size();
    }
};
ii、做法二:手动版本
cpp 复制代码
class Solution 
{
public:
    int findCircleNum(vector<vector<int>>& isConnected) 
    {
        vector<int> ufs(isConnected.size(), -1);

		// lambda表达式
        auto FindRoot = [&ufs](int x) 
        {
            while (ufs[x] >= 0) x = ufs[x];
            return x;
        };
        for (int i = 0; i < isConnected.size(); i++)
        {
            for (int 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;
    }
};

(3)演示题目:等式方程可满足性

leetcode题目链接跳转

进行两次遍历,第一次遍历假如说是中间是等号的情况下的话,就将俩字母都放到同一个集合中,第二次遍历假如说是中间是不等号的情况下的话,就判断俩字母是否是在同一个集合中,在的话就返回false,不在的话就返回true。

cpp 复制代码
class Solution 
{
public:
    bool equationsPossible(vector<string>& equations) 
    {
        vector<int> ufs(26, -1);

		// lambda表达式
        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;
    }
};
相关推荐
moringlightyn17 分钟前
c++11可变模版参数 emplace接口 新的类功能 lambda 包装器
开发语言·c++·笔记·其他·c++11·lambda·包装器
郝学胜-神的一滴22 分钟前
使用Linux系统函数递归遍历指定目录
linux·运维·服务器·开发语言·c++·软件工程
怎么没有名字注册了啊31 分钟前
求一个矩阵中的鞍点
数据结构·算法
仟千意1 小时前
数据结构:二叉树
数据结构·算法
会开花的二叉树1 小时前
C++微服务 UserServer 设计与实现
开发语言·c++·微服务
我星期八休息2 小时前
C++智能指针全面解析:原理、使用场景与最佳实践
java·大数据·开发语言·jvm·c++·人工智能·python
·白小白2 小时前
力扣(LeetCode) ——11.盛水最多的容器(C++)
c++·算法·leetcode
深思慎考3 小时前
RabbitMQ 入门:基于 AMQP-CPP 的 C++ 实践指南与二次封装
开发语言·c++·分布式·rabbitmq·api
深思慎考3 小时前
Ubuntu 系统 RabbitMQ 安装指南与使用(含 C++ 客户端与 SSL 错误解决)
c++·ubuntu·rabbitmq·github·rabbitmqpp
代码欢乐豆4 小时前
编译原理机测客观题(7)优化和代码生成练习题
数据结构·算法·编译原理