forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.cpp
79 lines (79 loc) · 1.09 KB
/
project.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
class myclass
{
int *rank, *parentvar;
int n;
public:
myclass(int n)
{
rank = new int[n];
parentvar = new int[n];
this->n = n;
sets();
}
void sets()
{
for (int i = 0; i < n; i++)
{
parentvar[i] = i;
}
}
int find(int x)
{
if (parentvar[x] != x)
{
parentvar[x] = find(parentvar[x]);
}
return parentvar[x];
}
void Union(int x, int y)
{
int xset = find(x);
int yset = find(y);
if (xset == yset)
return;
if (rank[xset] < rank[yset])
{
parentvar[xset] = yset;
}
else if (rank[xset] > rank[yset]) {
parentvar[yset] = xset;
}
else
{
parentvar[yset] = xset;
rank[xset] = rank[xset] + 1; }
}
};
int main()
{
myclass obj(6);
obj.Union(0, 1);
obj.Union(0, 3);
obj.Union(1, 2);
obj.Union(4, 5);
if (obj.find(2) == obj.find(1)) {
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
if (obj.find(0) == obj.find(1)) {
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
if (obj.find(4) == obj.find(1))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}