-
Notifications
You must be signed in to change notification settings - Fork 21
/
unionfind.cpp
64 lines (54 loc) · 1.92 KB
/
unionfind.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
//
// Created by light on 19-10-26.
//
#include "unionfind.h"
double testUF(UF uf, int m) {
int size = uf.getSize();
time_t start = clock();
// [0,size)范围随机数
for (int i = 0; i < m; i++) {
int a = rand() % size;
int b = rand() % size;
uf.unionElements(a, b);
}
for (int i = 0; i < m; i++) {
int a = rand() % size;
int b = rand() % size;
uf.isConnected(a, b);
}
time_t end = clock();
return double(end - start) / CLOCKS_PER_SEC;
}
int main() {
UnionFind1 unionFind1(10);
cout << unionFind1.getSize() << endl;
cout << unionFind1.isConnected(1, 2) << endl;
unionFind1.unionElements(1, 2);
cout << unionFind1.isConnected(1, 2) << endl;
UnionFind2 unionFind2(10);
cout << unionFind2.getSize() << endl;
unionFind2.unionElements(1, 2);
unionFind2.unionElements(7, 2);
cout << unionFind2.isConnected(1, 2) << endl;
cout << unionFind2.isConnected(1, 7) << endl;
cout << unionFind2.isConnected(2, 7) << endl;
unionFind2.unionElements(3, 8);
unionFind2.unionElements(4, 3);
unionFind2.unionElements(9, 4);
cout << unionFind2.isConnected(9, 8) << endl;
int size = 10000000;
int m = 10000000;
UnionFind1 unionFind11(size);
UnionFind1 unionFind22(size);
UnionFind3 unionFind33(size);
UnionFind4 unionFind44(size);
UnionFind5 unionFind55(size);
UnionFind6 unionFind66(size);
cout << "UnionFind 1 result is " << testUF(unionFind11, m) << " s." << endl;
cout << "UnionFind 2 result is " << testUF(unionFind22, m) << " s." << endl;
cout << "UnionFind 3 result is " << testUF(unionFind33, m) << " s." << endl;
cout << "UnionFind 4 result is " << testUF(unionFind44, m) << " s." << endl;
cout << "UnionFind 5 result is " << testUF(unionFind55, m) << " s." << endl;
cout << "UnionFind 6 result is " << testUF(unionFind66, m) << " s." << endl;
return 0;
}