Graph Theory37 sections · 1633 units
Open in Course

Naive Approach

(Jump one step at a time)

The obvious solution: store each node's parent, then jump up kk times. For each query, start at vv and follow the parent pointer kk times. This takes O(k)O(k) time per query. If k=n1k = n-1 (node is near a leaf, ancestor is the root), that is O(n)O(n) per query. With a million queries, that is a billion operations. Too slow.

You need a faster way to jump. The bottleneck: each jump moves you only one level up. What if you could skip multiple levels at once?