-
Notifications
You must be signed in to change notification settings - Fork 12
/
Disjoint Set Undo.cpp
55 lines (50 loc) · 1.02 KB
/
Disjoint Set Undo.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
/**
* Name : Dijoint Set with undo
* Description : DisjointSet (Makes a set of sets, merge sets, set membership, no. of sets, undo last operation,size of each component)
* Time Complexity : parent O(lg(N)), setUp O(lg(N)), undo O(1),
*/
#define MX 10000
int rp[MX],sz[MX];
int compo;
int pts[MX*2],in=0;
int parent(int n){
if(rp[n]==n)return n;
return rp[n]=parent(rp[n]);
}
// additionally storing parent which is connected to another parents
void setUp(int a,int b){
a = parent(a);
b = parent(b);
if(a==b){
pts[++in]=-1;
return;
}
if(sz[a]<sz[b]){
rp[a] = rp[b];
sz[b] += sz[a];
pts[++in]=a;
}
else{
rp[b] = rp[a];
sz[a] += sz[b];
pts[++in] = b;;
}
compo--;
}
void undo(){
if(!in) return;
int n = pts[in--];
if(n!=-1) {
sz[parent(rp[n])] -= sz[n];
rp[n]=n;
compo++;
}
}
void init(int n){
in=0;
for(int i=0;i<=MX;i++){
rp[i]=i;
sz[i]=1;
}
compo=n;
}