-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheck for Bipartite Graph
42 lines (41 loc) · 984 Bytes
/
Check for Bipartite Graph
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
#include<bits/stdc++.h>
bool solve(int start, vector<int> adj[], vector<int> color){
queue<int> q;
q.push(start);
color[start] = 1;
while(!q.empty()){
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[it] == color[node]){
return false;
}
}
}
return true;
}
bool isGraphBirpatite(vector<vector<int>> &edges) {
int n = edges.size();
vector<int> adj[n];
for(int i=0;i<n;i++){
for(int j=0;j<n;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(!solve(i,adj,color)){
return false;
}
}
}
return true;
}