Guess your way out problem


I am getting TLE as its obvious but how to optimise my code if its correct approach?
If its not correct approach then may I know the correct one.

@nayakashutosh99
tree



Please refer to this

Thanks a lot for the solution