Skip to content

Commit 68afa3e

Browse files
authored
Merge pull request hound77#26 from Dibya1405/master
Create Is Graph Bipartite.cpp
2 parents 8c72d1e + be626c2 commit 68afa3e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Is Graph Bipartite.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
4+
bool dfscol(int s, int color, vector<vector<int>>& graph, vector<int>& col){
5+
for(auto v : graph[s])
6+
{
7+
if(col[v]==-1)
8+
{
9+
col[v]=1-color;
10+
if(dfscol(v,1-color,graph,col)==false)
11+
return false;
12+
}
13+
else if(col[v]==color)
14+
return false;
15+
}
16+
return true;
17+
}
18+
19+
bool isBipartite(vector<vector<int>>& graph) {
20+
vector<int>col(graph.size(),-1);
21+
for(int i=0;i<graph.size();i++)
22+
{
23+
if(col[i]==-1)
24+
{
25+
col[i]=0;
26+
if(dfscol(i,0,graph,col)==false)
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
};

0 commit comments

Comments
 (0)