Here is my understanding of the question:
- The graph may contain more than 1 connected component.
- In every connected component there is a node/vertex with lowest value, this vertex is master vertex/parent in the CC. Eg a connected component contains vertices 3,4,5,1 so 1 will be the master parent of this CC, and this vertex will not have any parent.
- Given this info, i do the following:
3a. I start the loop from 1 till n, this is to start dfs and get components from master vertex. i.e vertex with lowest value.
3b. I maintain a connect component vector which stores vertices in that connected component.
3c. After getting parent and children of every vertex using DFS in the current CC, i check the condition that if parent of current node has more children than the current node then i increase the counter by 1. i.e i have found 1 beautiful vertex.
Please tell me where am i going wrong:
My Code: