What should be the approach for this problem?
please help
Mike and or tree
For a graph you are given some queries and following two operations
- update operation : make a node value to 1
- query operation : output the minimum distance of a node with from a node with value 1
Initially the node 1 has value 1 and all other node has value 0
Now , you can use a dfs based approach with a little mix of dp
you can have a dist array which will store the minimum distance of a node with some node with value 1 So, what you can do is whenever you make a node(say X) value to 1 start dfs considering it as root and traverse down the root increasing the level value by 1 whenever you go one level down, and take minimum of the dist[ child node] and level (relative of our X node)
and in query operation just output dist[node]
just for reference: https://ide.codingblocks.com/s/182041
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.