-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipartiteGraph.cpp
45 lines (40 loc) · 1015 Bytes
/
BipartiteGraph.cpp
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
39
40
41
42
43
44
45
class Solution {
private:
bool check(int start,int V, vector<int>adj[],vector<int>&colors){
queue<int>q;
q.push(start);
colors[start]=0;
while(!q.empty()){
int node=q.front();
q.pop();
for(auto it:adj[node]){
//if adj[node] is yet not color
if(colors[it]==-1){
colors[it]=!colors[node];
q.push(it);
}
// is the adjacent node have the same color
else if(colors[it]==colors[node]){
return false;
}
}
}
return true;
}
public:
bool isBipartite(int V, vector<int>adj[]){
// Code here
vector<int>colors(V);
for(int i=0;i<V;i++){
colors[i]=-1;
}
for(int i=0;i<V;i++){
if(colors[i]==-1){
if(check(i,V,adj,colors)==false){
return false;
}
}
}
return true;
}
};