V is the number of vertices
vector adj[ ] stores the information of vertex connectivity. Suppose, we have an example as : 1 0, 0 2, 2 1, 0 3, 3 4 where each pair indicate the connection. For this, we can see that the vertex 0 is a mother vertex, but the code is returning that there is no mother vertex. I cannot figure out the reason.
//Driver Function
void dfsUtil(int i,int vis[],vector<int> adj[])
{
vis[i]=1;
for(int j=0;j<adj[i].size();j++)
if(!vis[j])
dfsUtil(j,vis,adj);
}
int dfs(int i,int V,vector<int> adj[])
{
int vis[V];
for(int j=0;j<V;j++)
vis[j]=0;
dfsUtil(i,vis,adj);
for(int j=0;j<V;j++)
if(!vis[j])
return 0;
return 1;
}
int findMother(int V, vector<int> adj[])
{
for(int i=0;i<V;i++)
if(dfs(i,V,adj))
return i;
return -1;
}