题目描述:

解题思路:

代码:
//
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
vector<int> colors(graph.size(), UNKNOW);
for (int i = 0; i< graph.size(); i++){
if (colors[i] == UNKNOW){
colors[i] = RED;
queue<int> q{{i}};
while (q.size()){
int cur = q.front();
q.pop();
int next_color = colors[cur] == RED?BLUE:RED;
for (int next: graph[cur]){
if (colors[next] == UNKNOW){
colors[next] = next_color;
q.push(next);
} else if (colors[next] != next_color)
return false;
}
}
}
}
return true;
}
private:
enum {UNKNOW, RED, BLUE}; // 未限定作用域的 枚举类
};