Graph Theory37 sections · 1633 units
Open in Course

Reveal - Company Queries II

(Why LCA is the right choice)

This problem asks for the lowest common ancestor of two employees. No updates, no subtree ranges, no path counting. Ancestor queries.

LCA with Binary Lifting is perfect. You preprocess the tree once in O(nlogn)O(n \log n) time, storing ancestors at powers of 22 for each node. Then each LCA query takes O(logn)O(\log n).

With 200200,000000 queries, this runs comfortably within time limits. Naive O(n)O(n) per query would TLE.

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