-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path785-is-graph-bipartite.cpp
35 lines (33 loc) · 1.05 KB
/
785-is-graph-bipartite.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
#include <queue>
#include <vector>
class Solution {
public:
bool doBFS(int i, vector<int>& visited, vector<vector<int>>& graph) {
queue<int> q;
int currColor;
q.push(i);
visited[i] = 0; // Assign blue by default
while (!q.empty()) {
int currNode = q.front();
q.pop();
currColor = visited[currNode];
for (int child : graph[currNode]) {
if (visited[child] == currColor) return false;
if (visited[child] == -1) {
q.push(child);
visited[child] = 1 - currColor;
}
}
}
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
vector<int> visited(graph.size(), -1); // -1 : not visited, 0 : blue color, 1 : red color
for (int i = 0; i < graph.size(); i++) {
if (visited[i] == -1) {
if (!doBFS(i, visited, graph)) return false;
}
}
return true;
}
};