-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.cpp
More file actions
54 lines (52 loc) · 982 Bytes
/
UnionFind.cpp
File metadata and controls
54 lines (52 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "bits/stdc++.h"
using namespace std;
class UnionFind{
private: vector<int> szSet,p,rank;
int nDisSet;
public:
UnionFind(int N){
rank.assign(N,0);
p.assign(N,0);
szSet.assign(N,1);
nDisSet = N;
for(int i=0;i<N;i++) p[i]=i;
}
int findSet(int i){
if(p[i] == i) return i;
return p[i] = findSet(p[i]);
}
bool isSameSet(int i,int j){
return findSet(i) == findSet(j);
}
void unionSet(int i,int j){
if(!isSameSet(i,j)){
szSet --;
int x = findSet(i),y = findSet(j);
if(rank[x] > rank[y]){
p[y] = x;
szSet[x] += szSet[y];
}
else {
szSet[y] += szSet[x];
p[x] = y;
if(rank[x] == rank[y]) rank[y]++;
}
}
}
int numDisjointSets(){
return nDisSet;
}
int sizeOfSet(int i){
int x = findSet(i);
return szSet[x];
}
};
int main(int argc, char const *argv[])
{
UnionFind x(5);
x.unionSet(0,1);
x.unionSet(2,3);
x.unionSet(0,3);
cout<<x.findSet(0)<<endl;
return 0;
}