Graph Theory37 sections · 1633 units
Open in Course

Problem - Company Queries I

(CSES 1687 - k-th ancestor)

You are given a tree representing a company hierarchy. Each employee has a boss (parent), except the CEO (root). Given qq queries of the form (v,k)(v, k), find the kk-th boss of employee vv.

If vv does not have a kk-th boss (if kk exceeds the depth), return 1-1. This is the classic kk-th ancestor problem. Binary lifting solves it perfectly. Build the upup table once, then answer each query in O(logn)O(\log n) time.