MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / solve

Function solve

disjoint set - Copy/code.cpp:45–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43}
44
45void solve()
46{
47 int n = 8; // 0 - 7
48 vector<int> parent(n), rank(n, 0);
49 for (int i = 0; i < n; i++)
50 {
51 parent[i] = i;
52 }
53 int total;
54 cin >> total;
55 while (total--)
56 {
57 int choice;
58 cin >> choice;
59 if (choice == 1)
60 {
61 // union
62 int num1, num2;
63 cin >> num1 >> num2;
64 union_num(parent, rank, num1, num2);
65 }
66 else
67 {
68 // find
69 int num1, num2;
70 cin >> num1 >> num2;
71 int fparent = find(parent, num1), sparent = find(parent, num2);
72
73 if (fparent == sparent)
74 {
75 cout << "They are in same group!!" << endl;
76 }
77 else
78 {
79 cout << "They are not in same group!!" << endl;
80 }
81 }
82
83 cout << "Check parent array: " << endl;
84 for (int i = 0; i < parent.size(); i++)
85 {
86 cout << parent[i] << " ";
87 }
88 cout << endl;
89 cout << "Check Rank array: " << endl;
90 for (int i = 0; i < rank.size(); i++)
91 {
92 cout << rank[i] << " ";
93 }
94 cout << endl;
95 }
96}
97
98int main()
99{

Callers 1

mainFunction · 0.70

Calls 3

union_numFunction · 0.85
findFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected