-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path164. CheckBipartite.cpp
43 lines (40 loc) · 960 Bytes
/
164. CheckBipartite.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
#include<bits/stdc++.h>
bool bfs(int node, int V, vector<int>adj[], vector<int> &color)
{
color[node]=0;
queue<int> q;
q.push(node);
while(q.empty()==false)
{
int node = q.front();
q.pop();
for(auto it: adj[node]){
if(color[it]==-1){
color[it] = !color[node];
q.push(it);
}
else if(color[node] == color[it])return false;
}
}
return true;
}
bool isGraphBirpatite(vector<vector<int>> &edges) {
int n = edges.size();
int m = edges[0].size();
vector<int>adj[n];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(edges[i][j] == 1){
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
vector<int> color(n,-1);
for(int i=0;i<n;i++){
if(color[i]==-1){
if(bfs(i,n,adj,color)==false)return false;
}
}
return true;
}