disjoint set code in c++
C++
// C++ implementation of disjoint set
#include <iostream>
using namespace std;
class DisjSet {
int *rank, *parent, n;
public:
// Constructor to create and
// initialize sets of n items
DisjSet(int n)
{
rank = new int[n];
parent = new int[n];
this->n = n;
makeSet();
}
// Creates n single item sets
void makeSet()
{
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
// Finds set of given item x
int find(int x)
{
// Finds the representative of the set
// that x is an element of
if (parent[x] != x) {
// if x is not the parent of itself
// Then x is not the representative of
// his set,
parent[x] = find(parent[x]);
// so we recursively call Find on its parent
// and move i's node directly under the
// representative of this set
}
return parent[x];
}
// Do union of two sets represented
// by x and y.
void Union(int x, int y)
{
// Find current sets of x and y
int xset = find(x);
int yset = find(y);
// If they are already in same set
if (xset == yset)
return;
// Put smaller ranked item under
// bigger ranked item if ranks are
// different
if (rank[xset] < rank[yset]) {
parent[xset] = yset;
}
else if (rank[xset] > rank[yset]) {
parent[yset] = xset;
}
// If ranks are same, then increment
// rank.
else {
parent[yset] = xset;
rank[xset] = rank[xset] + 1;
}
}
};
int main()
{
DisjSet obj(5);
obj.Union(0, 2);
obj.Union(4, 2);
obj.Union(3, 1);
if (obj.find(4) == obj.find(0))
cout << "Yes\n";
else
cout << "No\n";
if (obj.find(1) == obj.find(0))
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
Also in C++:
- Title
- set precision in c++
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- root to leaf path print
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- runtime error in c++
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ vector add element
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- matrix transpose tiling
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- insertion sort in c++ program
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- gfg left view of tree
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- while loops
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- Newton's sqrt in c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++