-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 785.cpp
More file actions
38 lines (38 loc) · 957 Bytes
/
Leetcode 785.cpp
File metadata and controls
38 lines (38 loc) · 957 Bytes
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
36
37
38
class Solution {
private:
bool bfs(int start,int n,vector<vector<int>>& graph,int color[] ){
color[start]=0;
queue<int>q;
q.push(start);
while(!q.empty()){
int node=q.front();
q.pop();
for(auto it:graph[node]){
if(color[it]==-1){
color[it]=!color[node];
q.push(it);
}
else if(color[it]==color[node]){
return false;
}
}
}
return true;
}
public:
bool isBipartite(vector<vector<int>>& graph) {
int n=graph.size();
int color[n];
for(int i=0;i<n;i++){
color[i]=-1;
}
for(int i=0;i<n;i++){
if(color[i]==-1){
if(bfs(i,n,graph,color)==false){
return false;
}
}
}
return true;
}
};