Graph Theory37 sections · 1633 units
Open in Course

Reveal - Blood Cousins

(Two techniques work together)

This problem combines LCA and Euler Tour. First, use binary lifting to find the ancestor of v at depth h. Call this ancestor a.

Now you need to count nodes at depth d inside a's subtree. Use Euler Tour to flatten a's subtree into a range. Then count how many nodes in that range have depth exactly d.

You can preprocess this with a data structure (like a map of depth → sorted list of Euler Tour positions) to answer each query in O(logn)O(\log n).

Space complexity is O(n)O(n) for the data structures used.