class Solution {
public:
bool DFS(int node,int cc,vector<vector<int>>&adj,vector<int>&color,vector<bool>&visited)
{
visited[node]=1;
for(int x:adj[node]){
if(color[x]==cc)return false;
}
color[node]=cc;
int nextc=0;
if(!cc)nextc=1;
for(int x:adj[node]){
if(!visited[x])if(!DFS(x,nextc,adj,color,visited))return false;
}
return true;
}
bool isBipartite(int V, vector<vector<int>> &edges) {
// Code here
vector<vector<int>>adj(V);
for(auto e:edges){
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
vector<int>color(V,-1);
vector<bool>visited(V,false);
return DFS(0,0,adj,color,visited);
}
};
No comments:
Post a Comment