Graph Theory37 sections · 1633 units
Open in Course

Problem - Company Queries II

(CSES 1688)

You are given a tree representing company hierarchy. Answer queries: who is the lowest common ancestor (LCA) of two employees?

Euler tour full variant solves this. Record every node during DFS (including revisits). LCA of uu and vv is the shallowest node between their first occurrences in the tour. Use RMQ (range minimum query) on depths to find LCA in O(logn)O(\log n) per query after O(nlogn)O(n \log n) preprocessing.

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