Graph Theory37 sections · 1633 units
Open in Course

Problem - Company Queries II

(CSES Tree, LCA application)

You are given a tree representing a company hierarchy. Each employee except the boss has exactly one direct supervisor. The tree is rooted at the boss. Answer queries: what is the lowest common ancestor of employees a and b?

In other words, who is their closest common supervisor in the hierarchy?

There are up to 200200,000000 employees and 200200,000000 queries. You need fast lookups. Binary lifting gives you O(logn)O(\log n) per query after O(nlogn)O(n \log n) preprocessing.