From 59aa1556727aa81cf4c1ba77a2d6b35d3ac61b1f Mon Sep 17 00:00:00 2001 From: Aayush Vyas <70998175+AayushVyasKIIT@users.noreply.github.com> Date: Sat, 28 Aug 2021 14:28:31 +0530 Subject: [PATCH] made changes to satisfy the meets of the PR changes made to satisfy the needs of the PR - used base lib instead of - changed all stdin's and included pre-declared test cases --- data_structures/DSU_path_compresssion.cpp | 39 +++++++++++----------- data_structures/DSU_union_rank.cpp | 40 +++++++++++++++++------ 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/data_structures/DSU_path_compresssion.cpp b/data_structures/DSU_path_compresssion.cpp index 51d62706c..b0112d29e 100644 --- a/data_structures/DSU_path_compresssion.cpp +++ b/data_structures/DSU_path_compresssion.cpp @@ -1,9 +1,8 @@ -#include +#include +#include using namespace std; -#define vi vector - //Disjoint set union class DSU{ private: @@ -12,7 +11,7 @@ class DSU{ // setSize: size of each chunk(set) // maxElement : max of each set, using maxElement[representative] // minElement : min of each set, using minElement[representative] - vi p,depth,setSize,maxElement,minElement; + vector p,depth,setSize,maxElement,minElement; public: // parameter : int n -> maximum number of items DSU(int n){ @@ -83,7 +82,7 @@ class DSU{ } //returns min max size of i's set void get(int i){ - cout << get_min(i) << " " << get_max(i) << " " <>n;cin>>q; + int n,q; + n = 10; q = 11; //n: number of items //q: number of queries to be made DSU d(n+1); + //set 1 + cout << "set 1:"<>op; - if(op == "union"){ - int i,j; - cin >> i >> j; - d.UnionSet(i,j); //performs union operation on i and j - }else{ - int i; - cin >> i; - d.get(i); //print min max and size of set. - } - } + //set 2 + cout << "\nset 2"< +#include +#include + using namespace std; -#define vi vector class DSU{ private: // p: keeps track of parent of i // depth: tracks the depth of i // setSize: size of each chunk(set) - vi p,depth,setSize; + vector p,depth,setSize; public: //parameter : int n -> maximum number of items DSU(int n){ @@ -61,16 +62,35 @@ class DSU{ } return false; } + void getParents(int i){ + while(p[i]!=i){ + cout << i << " ->"; + i = p[i]; + } + cout << p[i] << endl; + } }; int main(){ - //number of items - int n;cin>>n; - + int n; + n = 10; + //n: number of items DSU d(n+1); - //perform operations here - d.unionSet(2,4); - d.unionSet(3,4); - cout << d.findSet(2) <