-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion_set.cpp
More file actions
38 lines (30 loc) · 875 Bytes
/
union_set.cpp
File metadata and controls
38 lines (30 loc) · 875 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
#include<bits/stdc++.h>
using namespace std;
int find(int v, vector<int> & p) {
if(p[v] == v) return v;
return p[v] = find(p[v], p);
}
int uni(const int & a, const int & b, vector<int> & p) {
if(rand() & 1) return p[find(a,p)] = find(b, p);
return p[find(b,p)] = find(a, p);
}
int main() {
srand(time(NULL));
int n = 10;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
cout << uni(1, 2, p) << endl;
cout << find(1, p) << endl;
cout << uni(2, 3, p) << endl;
cout << uni(1, 4, p) << endl;
cout << uni(1, 0, p) << endl;
cout << find(0, p) << endl;
cout << uni(5, 9, p) << endl;
cout << find(9, p) << endl;
cout << uni(5, 6, p) << endl;
cout << uni(7, 6, p) << endl;
cout << uni(7, 8, p) << endl;
cout << find(8, p) << endl;
cout << uni(3, 7, p) << endl;
cout << find(8, p) << endl;
}