Beautiful vertices : graph

this code is giving correct answer for sample testcase.
But not passing other test cases due to timelimit


plzz suggest something

This question is an interesting implementation of DFS. Perform DFS while maintaining a count of children of the vertices that you have visited.
Let u be a vertex and v be its child. Then, check if children[v]>children[u].This way, check for each vertex while traversing the graph in DFS.

NOTE:
Actually there are many trees in the input(a graph with no cycles and self loops is a tree). That means , you have to apply DFS for each of these trees.

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.