here’s my code.
#include
#include
#include
#include
#include
#include
using namespace std;
class Pair
{
public:
int parent;
int child;
};
class Graph
{
public:
int n;
map< int,list > m;
Graph(int n)
{
this->n = n;
}
void addEdge(int u, int v)
{
m[u].push_back(v);
m[v].push_back(u);
}
void Beautiful_Vertices()
{
map<int,bool> visited;
map<int,Pair > family;
for(int i = 1; i<=n; i++)
{
family[i].parent = 0;
family[i].child = 0;
visited[i] = false;
}
int componenet = 0;
for(auto i:m)
{
int node = i.first;
if(!visited[node])
{
DFS_Helper(node,visited,family);
componenet++;
}
}
int vertices = 0;
for(auto i:m)
{
// cout<<i.first<<" parent: "<<family[i.first].parent <<" children: "<<family[i.first].child<<endl;
if((family[i.first].parent < family[i.first].child))
{
vertices++;
}
}
cout<<vertices;
}
//family--- first=parent and second is child
void DFS_Helper(int node, map<int,bool> &visited, map<int,Pair > &family)
{
visited[node] = true;
for(auto i:m[node])
{
if( !visited[i] )
{
family[node].child++;
// cout<<node<<" node and i :"<<i<<endl;
DFS_Helper(i,visited, family);
//cout<<"yess "<<i<<endl;
}
else if(visited[i])
{
//cout<<" i : node "<<i<<node<<endl;
family[node].parent++;
}
// cout<<i<<" hehe "<<endl;
}
return;
}
};
int main() {
int n,m; cin>>n>>m;
Graph g(n);
for(int i=0; i<m; i++)
{
int u,v; cin>>u>>v;
g.addEdge(u,v);
}
g.Beautiful_Vertices();
return 0;
}
please tell what’s the correct answer.