Skip to content

Commit

Permalink
DSU updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zarif98sjs committed Aug 16, 2021
1 parent efd2e39 commit e7c1656
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Data Structure/12 Disjoint Set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,30 @@ inline void optimizeIO()
cin.tie(NULL);
}

/**
*
* Disjoint Set
* 1 based indexing
*
**/

struct DisjointSet
{
unordered_map<int, int> parent;
vector<int>sz;
int comp;

vector<vector<int>>g; /// extra part

void makeSet(int N)
{
sz = vector<int>(N+1,1);
g = vector<vector<int>>(N+1); /// extra part

for (int i = 1; i <= N; i++)
parent[i] = i;
parent[i] = i, g[i].push_back(i);

comp = N;
}

int Find(int k)
Expand All @@ -59,15 +75,31 @@ struct DisjointSet
int x = Find(a);
int y = Find(b);

if(x==y) return;

/**
merge is done according to size
if we do that, when we maintain adjacency list, we can merge small to large component
this optimizes the union process
**/

if(sz[x] < sz[y]) swap(x,y);

sz[x] += sz[y];
parent[y] = x;

/// extra part
g[x].insert(g[x].end(), ALL(g[y]));
g[y].clear();

comp--;
}

void printParent(int N)
{
for (int i = 1; i <= N; i++)
cout<<parent[i]<<" ";
cout<<endl;

}
};

Expand Down

0 comments on commit e7c1656

Please sign in to comment.