Is Graph Bipartite? LeetCode solution in C++

1
class Solution {
2
public:
3
bool isBipartite(int node, vector<int> &color, vector<vector<int>> &graph) {
4
if (color[node] == -1) color[node] = 1;
5
for (auto it : graph[node]) {
6
if (color[it] == -1) {
7
color[it] = !color[node];
8
if (!isBipartite(it, color, graph)) return false;
9
} else if (color[it] == color[node])
10
return false;
11
}
12
return true;
13
}
14
bool isBipartite(vector<vector<int>> &graph) {
15
int n = graph.size();
16
vector<int> color(n, -1);
17
for (int i = 0; i < n; i++)
18
if (color[i] == -1)
19
if (!isBipartite(i, color, graph)) return false;
20
return true;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0