Skip to content

Commit

Permalink
Create 19 May | 785. Is Graph Bipartite?.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SumitPareek2401 authored May 19, 2023
1 parent 8f255ab commit 3730204
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 19 May | 785. Is Graph Bipartite?.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> color(n,0);

for(int node = 0; node < n; node++){
if(color[node] != 0) continue;

queue<int> q;
q.push(node);
color[node] = 1;

while(!q.empty()){
int curr = q.front();
q.pop();

for(int adjN : graph[curr]){
if(color[adjN] == 0){
color[adjN] = -color[curr];
q.push(adjN);
}
else if(color[adjN] != -color[curr]){
return false;
}
}
}
}
return true;
}
};

0 comments on commit 3730204

Please sign in to comment.