-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path886.cpp
More file actions
36 lines (36 loc) · 1.21 KB
/
886.cpp
File metadata and controls
36 lines (36 loc) · 1.21 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
class Solution {
public:
bool partitionable(int node, vector<vector<int>>& adjacency, vector<int>& colors) {
queue<int> q;
q.push(node);
colors[node] = 1;
while (!q.empty()) {
int current = q.front();
q.pop();
int currentColor = colors[current];
int nextColor = 3 - currentColor;
for (auto& neighbor : adjacency[current]) {
if (colors[neighbor] != 0 && colors[neighbor] != nextColor) return false;
if (colors[neighbor] == 0) {
q.push(neighbor);
colors[neighbor] = nextColor;
}
}
}
return true;
}
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
vector<vector<int>> adjacency(n);
for (auto& dislike : dislikes) {
adjacency[dislike[0] - 1].push_back(dislike[1] - 1);
adjacency[dislike[1] - 1].push_back(dislike[0] - 1);
}
vector<int> colors(n, 0);
for (int i = 0; i < n; ++i) {
if (colors[i] == 0) {
if (!partitionable(i, adjacency, colors)) return false;
}
}
return true;
}
};