Given that the style of input may vary for different types of questions, could you please provide me ways to approach graph problems?
It would be helpful if you could just share the solution to the problem.
Given that the style of input may vary for different types of questions, could you please provide me ways to approach graph problems?
It would be helpful if you could just share the solution to the problem.
hey, there s no list for approaches. you just need to solve questions and learn from them
The problem is simple implementation of DFS while maintaining a count of children of the vertices 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. One important thing to note that 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.
try to implement this
Hii, I tried to implement the same, but still am getting wrong answer, could you maybe check where am i going wrong? Thank You!!
u should declare new map for parent and child for each component instead of using older map becuase they will have previous result as well .
you can check this out for better explanation:
Thank You for helping me out. I figured out the problem using Array List implementation of graph, is there any other better solution for the same?