please debug my code . https://ide.codingblocks.com/s/289741
Run time error!
There were many mistakes i have made few changes plz look.
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class graph{
public:
list<int>*l;
int v;
graph(int v)
{
this->v = v;
l = new list<int>[v+1];
}
void addedge(int x, int y)
{
//cout<<x<<y;
l[x].push_back(y);
l[y].push_back(x);
}
};
void dfs(int* parent , int* children , int curr_node , int* visited , graph g)
{
visited[curr_node]=1;
for(auto it : g.l[curr_node])
{
if(!visited[it])
{
parent[it]=curr_node;
children[curr_node] += 1;
dfs(parent,children,it,visited,g);
}
}
}
int main()
{
int n,m,x,y;
cin>>n>>m;
graph g(n);
for(int k=0;k<m;k++)
{
cin>>x>>y;
g.addedge(x,y);
}
int parent[n+1],children[n+1];
int visited[n+1];
memset(visited,0,sizeof(visited));
memset(parent,0,sizeof(parent));
memset(children,0,sizeof(children));
for(int i=1;i<=n;i++)
{
if(!visited[i])
{
dfs(parent,children,i,visited,g);
}
}
int ans =0;
for(int i=1;i<=n;i++)
{
int ans =0;
if(parent[i]!=0 && children[i]>children[parent[i]])
{
ans++;
}
}
cout<<ans<<endl;
}
sir can you please comment out errors in my code. I am not able to figure out my mistake.s Moreover you syntax is quite different from what i’ve written in my code.https://ide.codingblocks.com/s/289741
Graph was made till n it had to be made till n+1 and children and parent array had to be initialised to 0 . That’s it . Check the constructor for graph and also the 2 more memsets I added . These 3 changes only