Editorial for GFSSOC '14 Winter J5 - Pursuit of Knowledge
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
If you tried to run a BFS for each query, it is likely your code would've failed (unless it was in C++ or PyPy with good optimization). This is because the time cost is , where is a ridiculously huge number. Notice that there are many more queries than possible source nodes. Therefore, all we need to do is run BFS from each possible source node and store the values in an answer array. For each query, just grab the value from the answer array. In total, this solution takes time, a significant upgrade from the first.
Skills needed: BFS
Time complexity:
Note: The difficulty curve from J4 and J5 is quite steep. This question requires an algorithm known as Breadth First Search, which you can search up online.
Comments
You can still pass with BFSing each query if you stop searching as soon as you find the end node
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
I think that the editorial is only to give you the idea to solve the problem.