Graph Theory37 sections · 1633 units
Open in Course

Problem - Company Queries II

(CSES 1688 - LCA)

Same company tree as before. Now, given two employees aa and bb, find their lowest common boss (LCA). The LCA is the deepest node that is an ancestor of both aa and bb. This is the first shared boss in their chains to the CEO.

You will use binary lifting to solve this in O(logn)O(\log n) per query. First equalize depths, then binary search upward for the divergence point. The upup table you already built for kk-th ancestor works here too.