C++实现并查集

所属分类: 软件编程 / C 语言 阅读数: 63
收藏 0 赞 0 分享

本文实例为大家分享了C++实现并查集的具体代码,供大家参考,具体内容如下

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

class UnionFind{
private:
  vector<int> parent;
  int count;
  //优化,记录p和q所在组的深度,在合并时将深度小的结点的根指向深度大的结点的根
  vector<int> rank; 

public:
  UnionFind(int count){
    parent.resize(count);
    rank.resize(count);
    this->count = count;
    for(int i = 0; i < count; ++i){
      parent[i] = i;
      rank[i] = 1;
    }
  }
  ~UnionFind(){
    parent.clear();
    rank.clear();
  }
  //路径压缩
  int find(int p){
    assert(p >= 0 && p < count);
    if(p != parent[p])
      parent[p] = find(parent[p]);
    return parent[p];
  }
  bool isConnected(int p, int q){
    return find(p) == find(q);
  }
  void unionElement(int p, int q){
    int pRoot = find(p), qRoot = find(q);
    if(pRoot == qRoot)
      return;
    if(rank[pRoot] < rank[qRoot])
      parent[pRoot] = qRoot;
    else if(rank[qRoot] < rank[pRoot])
      parent[qRoot] = pRoot;
    else{
      //两者的rank相等
      parent[pRoot] = qRoot;
      rank[qRoot] += 1;
    }
  }
};

小编再补充一段代码,之前收藏的一段代码:

#include <iostream>

using namespace std;
class UF  {
 //cnt is the number of disjoint sets.
 //id is an array that records distinct identity of each set,when two sets are merged ,their id will be same.
 //sz is an array that records the child number of each set including the set self.
 int *id, cnt, *sz;
public:
 // Create an empty union find data structure with N isolated sets.
 UF(int N)  {
 cnt = N;
 id = new int[N];
 sz = new int[N];
 for (int i = 0; i<N; i++) {
  id[i] = i;
  sz[i] = 1;
 }
 }
 ~UF() {
 delete[] id;
 delete[] sz;
 }
 // Return the id of component corresponding to object p.
 int find(int p) {
 
 if (p != id[p]){
  id[p] = find(id[p]);
 }
 return id[p];
 }
 // Replace sets containing x and y with their union.
 void merge(int x, int y) {
 int i = find(x);
 int j = find(y);
 if (i == j) return;

 // make smaller root point to larger one
 if (sz[i] < sz[j]) {
  id[i] = j;
  sz[j] += sz[i];
 }
 else {
  id[j] = i;
  sz[i] += sz[j];
 }
 cnt--;
 }
 // Are objects x and y in the same set?
 bool connected(int x, int y)  {
 return find(x) == find(y);
 }
 // Return the number of disjoint sets.
 int count() {
 return cnt;
 }
};

void main(){
 UF test(5);
 test.merge(2, 3);
 test.merge(3, 4);
 cout << test.find(4);
 cout << test.count();
}

同时谢谢这位作者的分享

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

在C语言中对utmp文件进行查找和写入操作的函数小结

这篇文章主要介绍了在C语言中对utmp文件进行查找和写入操作的函数小结,包括pututline()函数和getutline()函数以及getutid()函数,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程中从密码文件获取数据的函数总结

这篇文章主要介绍了C语言编程中从密码文件获取数据的函数总结,包括getpw()函数和getpwnam()函数以及getpwuid()函数,需要的朋友可以参考下
收藏 0 赞 0 分享

在C语言编程中设置和获取代码组数的方法

这篇文章主要介绍了在C语言编程中设置和获取代码组数的方法,分别为setgroups()函数和getgroups()函数的使用,需要的朋友可以参考下
收藏 0 赞 0 分享

使用C语言操作文件的基本函数整理

这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下
收藏 0 赞 0 分享

简要对比C语言中的dup()函数和dup2()函数

这篇文章主要介绍了简要对比C语言中的dup()函数和dup2()函数,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中对文件最基本的读取和写入函数

这篇文章主要介绍了C语言中对文件最基本的读取和写入函数,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中lseek()函数和fseek()函数的使用详解

这篇文章主要介绍了C语言中lseek()函数和fseek()函数的使用详解,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言新建临时文件和临时文件名的方法

这篇文章主要介绍了C语言新建临时文件和临时文件名的方法,分别是mkstemp()函数和mktemp()函数的使用,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言的getc()函数和gets()函数的使用对比

这篇文章主要介绍了C语言的getc()函数和gets()函数的使用对比,从数据流中一个是读取字符一个是读取字符串,需要的朋友可以参考下
收藏 0 赞 0 分享

简单对比C语言中的fputs()函数和fputc()函数

这篇文章主要介绍了简单对比C语言中的fputs()函数和fputc()函数,注意其之间的区别,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多