I assume the solution demands a logn solution per query....
Here's the link of the problem http://www.spoj.com/problems/INCEST/
Here's my solution... https://ideone.com/1Ou6WQ
I am getting TLE
The complexity of my solution O(qlogn+nlogn+n)
nlogn->building the parent array to find LCA in logn time
n->dfs to find the start and finish time of each node
qlogn->logn for each query
Can anyone help me regarding this... Is there any other idea to solve the problem.....
Or ways to reduce the time complexity of the solution
Thank you very much!!!