https://cp-algorithms.com/data_structures/sqrt_decomposition.html
here’s the link of the ds…simple square root decomposition on array
I filled a table with all the nodes that I can go from any u, divided into sqrtN different segments for improving precalculation and query time (skipping the subtrees obviously for that I just checked using time-in and time-out technique in O(1))
moreover for getting to the right u also I used the same sqrtN algo
and the first step was making vector pii {h,v} for every v and then sorting this…further you can refer to my code