In san=mple test case, 1 has 0 parent and 1 chidren, also 2 has 1 parent and 2 children, so shouldn’t ans be 2?
Beautiful vertices
Hello @coolio.boy385
The edges that are provided in this question are NOT the final edges.
Example:-
7 5
2 1
3 1
4 3
6 5
6 7
Here in this input we can see that there are 2 connected components.
In the first connected component {1, 2, 3}, 1 is the master vertex (“In any connected component of the graph, vertex with the lowest value in that component serves as the master parent” given in the question) so there are NO incoming edges on vertex 1 but only outgoing edges.
So edge 2 1 is like 1->2, edge 3 1 is like 1->3, and egde 4 3 is like 3->4
Similarly in the second component {5, 6, 7}, 5 is the master vertex and edge 6 5 is assigned direction 5->6 and edge 6 7 is 6->7.
Modify your code to assign directions to the edges as above and then solve the problem.
The code
In the above code
To find the directions assigned to each edge, we start from the master vertex and for each connective edge i
we push realList[MasterVertex].push_back(i) to the real list, then we do the same for their children.
I have used the set to find the master vertex of each component.
Thanks, very well explained and I implemented this code https://ide.codingblocks.com/s/164462 , sample test case passed but got wrong ans
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.