-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2493.cpp
More file actions
84 lines (77 loc) · 2.66 KB
/
2493.cpp
File metadata and controls
84 lines (77 loc) · 2.66 KB
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
class Solution {
public:
int normalBfs(int start, unordered_map<int, unordered_set<int>>& hashEdges, vector<bool> visited) {
queue<int> q;
q.push(start);
visited[start] = true;
int level = 0;
while (!q.empty()) {
int n = q.size();
for (int i = 0; i < n; ++i) {
int node = q.front();
q.pop();
for (auto& neighbor : hashEdges[node]) {
if (visited[neighbor]) continue;
q.push(neighbor);
visited[neighbor] = true;
}
}
level++;
}
return level;
}
int checkConnectedBfs(int start, unordered_map<int, unordered_set<int>>& hashEdges, vector<bool>& visited) {
vector<int> members;
vector<bool> visitedCopy = visited;
queue<int> q;
q.push(start);
members.push_back(start);
visited[start] = true;
int level = 0;
while (!q.empty()) {
int n = q.size();
vector<int> collections;
for (int i = 0; i < n; ++i) {
int node = q.front();
q.pop();
for (auto& neighbor : hashEdges[node]) {
if (visited[neighbor]) continue;
collections.push_back(neighbor);
visited[neighbor] = true;
}
}
// check if all nodes in the same level have any connection;
int m = collections.size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < i; ++j) {
if (hashEdges[collections[i]].count(collections[j])) return -1;
}
}
for (int i = 0; i < m; ++i) {
q.push(collections[i]);
members.push_back(collections[i]);
}
level++;
}
for (auto& member : members) {
level = max(level, normalBfs(member, hashEdges, visitedCopy));
}
return level;
}
int magnificentSets(int n, vector<vector<int>>& edges) {
unordered_map<int, unordered_set<int>> hashEdges;
for (auto& edge : edges) {
hashEdges[edge[0] - 1].insert(edge[1] - 1);
hashEdges[edge[1] - 1].insert(edge[0] - 1);
}
vector<bool> visited(n, false);
int maxRes = 0;
for (int node = 0; node < n; ++node) {
if (visited[node]) continue;
int result = checkConnectedBfs(node, hashEdges, visited);
if (result == -1) return -1;
maxRes += result;
}
return maxRes;
}
};