-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsu.cpp
More file actions
43 lines (41 loc) · 673 Bytes
/
dsu.cpp
File metadata and controls
43 lines (41 loc) · 673 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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int arr[N];
int size[N];
void initialize(){
for(int i=0;i<N;++i){
arr[i]=i;
size[i]=1;
}
}
int root(int i){
while(arr[i]!=i){
i=arr[i];
}
return i;
}
void union(int a,int b){
int root_a = root(a);
int root_b = root(b);
if(size[root_a]>size[root_b]){
arr[root_b]=arr[root_a];
size[root_a]+=size[root_b];
}
else{
arr[root_a]=arr[root_b];
size[root_b]+=size[root_a];
}
}
bool find( int A, int B)
{
if(arr[ A ] == arr[ B ])
return true;
else
return false;
}
int main(int argc, char const *argv[])
{
// implementation of disjoint set union
return 0;
}