CHEFPRES BFS and LCA implementation

I am using BFS to find distance of each city from king’s city(capital).Then for each query, i am comparing the distance of LCA of chef’s city and each city having product p.Problem link.

Here is my solution.

A solution implemented in the same way got accepted.Check this solution.

Where am i going wrong ? Both the codes are implemented in nearly same way.

You are not using dist[] and prev[] to calculate LCA. tracing entire path is consuming too much time and not required.

Check mine LCA for more info:

1 Like

you are doing the samething.
Here is my code
Please check and suggest the reason for TLE.

Thanks btw.