Beautiful vertices

i am not able to solve this question. can you explain me the logic i tried but i think i have implemented the wrong logic.
here is my code

hello @aditikandhway the logic for the code i am explaining :
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.
here for your reference i am attaching the code in the most simple code .


Happy Learning !!