-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion_find.cpp
More file actions
44 lines (40 loc) · 774 Bytes
/
union_find.cpp
File metadata and controls
44 lines (40 loc) · 774 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
#include <iostream>
#include <vector>
using namespace std;
int getRoot(int * A, int i) {
while(A[i] != i) {
i = A[i];
}
return i;
}
void Union(int a, int b, int * A) {
int rootA = getRoot(A, a);
int rootB = getRoot(A, b);
A[rootA] = rootB;
}
bool find(int a, int b, int * A) {
if(getRoot(A, a) == getRoot(A, b)) {
cout << a << " and " << b << " are connected" << endl;
} else {
cout << a << " and " << b << " not connected" << endl;
}
}
int main() {
int A[10];
for(int i = 0; i < 10; i++) {
A[i] = i;
}
Union(0, 1, A);
Union(1, 2, A);
Union(2, 3, A);
Union(3, 4, A);
Union(4, 5, A);
Union(5, 6, A);
Union(6, 7, A);
Union(7, 8, A);
Union(8, 9, A);
find(0, 9, A);
find(9, 0, A);
find(0, 0, A);
return 0;
}