-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet204.cpp
More file actions
65 lines (59 loc) · 2 KB
/
leet204.cpp
File metadata and controls
65 lines (59 loc) · 2 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
class Solution {
vector<int> color; // Tracks bipartition colors (0/1)
vector<vector<int>> adj; // Adjacency list
int n; // Number of nodes
// DFS to check bipartiteness and collect component nodes
bool isBipartite(int node, int c, vector<int>& component) {
color[node] = c;
component.push_back(node);
for (int nbr : adj[node]) {
if (color[nbr] == c) return false; // Odd cycle detected
if (color[nbr] == -1 && !isBipartite(nbr, 1 - c, component))
return false;
}
return true;
}ups) for a component
int maxGroupsInComponent(const vector<int>& component) {
int maxDepth = 0;
for (int start : component) {
vector<int> depth(n, -1);
queue<int> q;
q.push(start);
depth[start] = 0;
while (!q.empty()) {
int node = q.front(); q.pop();
for (int nbr : adj[node]) {
if (depth[nbr] == -1) {
depth[nbr] = depth[node] + 1;
maxDepth = max(maxDepth, depth[nbr]);
q.push(nbr);
}
}
}
}
return maxDepth + 1; // Depth to groups conversion
}
public:
int magnificentSets(int n, vector<vector<int>>& edges) {
this->n = n;
color.assign(n, -1);
adj.resize(n);
for (auto& e : edges) {
int u = e[0] - 1, v = e[1] - 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<vector<int>> components;
for (int i = 0; i < n; ++i) {
if (color[i] == -1) {
components.emplace_back();
if (!isBipartite(i, 0, components.back()))
return -1; // Non-bipartite component
}
}
int total = 0;
for (auto& comp : components)
total += maxGroupsInComponent(comp);
return total;
}
};