Test case failing

I applied simple bfs and stored the parent and count of direct childs. NOT subtree chhild.

Then simple check if par[x] is having less child that x ans++
test case failing

the problem in your code is: you assumed that the graph is a single connected component.
but the question does not say so.
your solution is finding the number of beautiful vertices in a component of graph that contains vertex no.1…
generalize your code a little bit more, so that it also counts beautiful vertices from other components. and in other components the master node is the node with least value.

thanks

Well caught. Thanks lot

please rate it and resolve it…
thanks