-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisjointSetUnion.cpp
62 lines (55 loc) · 1.3 KB
/
DisjointSetUnion.cpp
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
55
56
57
58
59
60
61
vector<int> dsu; // the i'th index stores the parent of i if v[i] is positive, if v[i] is negative then i is its own parent...
int fin(int a){
vector<int> store;
while(dsu[a]>0){
store.push_back(a);
a=dsu[a];
}
for(int i=0;i<store.size();i++){
dsu[store[i]]=a;
}
return a;
}
void unio(int a,int b){
a=fin(a);
b=fin(b);
if(a!=b){
dsu[a]+=dsu[b];
dsu[b]=a;
}
}
int main(){
dsu.resize(n+1,-1);
dsu[0]=0;
for(int i=0;i<m;i++){
cin>>a>>b;
unio(a,b);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class DSU{
private:
vector<int> parent;
public:
DSU(int n){
parent.resize(n);
}
void setParent(int ind,int val){
parent[ind]=val;
}
int findFunc(int ind){
if(ind!=parent[ind]){
parent[ind]=findFunc(parent[ind]);
}
return parent[ind];
}
bool unionFunc(int ind1, int ind2){
int parentInd1=findFunc(ind1);
int parentInd2=findFunc(ind2);
if(parentInd1==parentInd2){
return false;
}
parent[parentInd1]=parentInd2;
return true;
}
};