class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
unordered_map<int,int> map;
queue<int> q;
for(int i = 0; i < graph.size(); i++){
if(!map[i]){
map[i] = 1;
q.push(i);
}
while(!q.empty()){
int f = q.front();
q.pop();
for(int i = 0; i < graph[f].size(); i++){
int v = graph[f][i];
if(map[v] == map[f])
return false;
if(!map[v]){
map[v] = -map[f];
q.push(v);
}
}
}
}
return true;
}
};
图 二分图 785. Is Graph Bipartite? (Medium)
最新推荐文章于 2022-06-24 11:34:18 发布