-
Notifications
You must be signed in to change notification settings - Fork 1
/
zad11_ms.cpp
91 lines (79 loc) · 1.77 KB
/
zad11_ms.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
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include "fu.cpp"
using namespace std;
vector<vector<int>> groups(const vector<vector<bool>> phoneNumbers)
{
int n = (int)phoneNumbers.size();
vector<find_union<int>> fus(n);
for (int i = 0; i < n; i++)
fus[i] = create_fu(i);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
continue;
if (!phoneNumbers[i][j])
{
if (!equivalent(fus[i], fus[j]))
{
union_fu(fus[i], fus[j]);
}
}
}
}
vector<bool> wasPrinted(n, false);
vector<vector<int>> groups;
for (int i = 0; i < n; i++)
{
if (wasPrinted[i])
continue;
find_union<int> current = fus[i];
vector<int> group;
for (int worker : elements(current))
{
wasPrinted[worker] = true;
int workerNo = worker+1;
group.push_back(workerNo);
}
groups.push_back(group);
}
for (int i = 0; i < n; i++)
{
free(fus[i]);
}
fus.clear();
return groups;
}
int main()
{
int n;
cin >> n;
vector<vector<bool>> phoneNumbers(n, vector<bool>(n, false));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int b;
cin >> b;
phoneNumbers[i][j] = (bool)b;
}
}
vector<vector<int>> gr = groups(phoneNumbers);
for (vector<int> group : gr)
{
for (int c : group)
{
cout << c << " ";
}
cout << endl;
group.clear();
}
gr.clear();
for (vector<bool> row : phoneNumbers)
{
row.clear();
}
phoneNumbers.clear();
return 0;
}