-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite Graph.cpp
More file actions
32 lines (28 loc) · 847 Bytes
/
Bipartite Graph.cpp
File metadata and controls
32 lines (28 loc) · 847 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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/bipartite-graph/1
*/
class Solution {
private:
bool dfs(int col, int node, int color[], vector<int> adj[]){
color[node]=col;
for(auto adjNode: adj[node]){
if(color[adjNode] == -1){
if(dfs(!col, adjNode, color, adj) == false) return false;
}
else if(color[adjNode] == col) return false;
}
return true;
}
public:
bool isBipartite(int V, vector<int>adj[]){
// using two colors 0 & 1 while -1 for unvisited
int color[V];
for(int i=0; i<V; i++) color[i]=-1; // auto for loop gives wrong ans!!
for(int i=0; i<V; i++){
if(color[i] == -1){
if(dfs(0, i, color, adj) == false) return false;
}
}
return true;
}
};