Graph Theory37 sections · 1633 units
Open in Course

Walkthrough

(Sample input trace)

Say n=5n = 5, tree: 123451 \to 2 \to 3 \to 4 \to 5 (a chain). Query: (5,3)(5, 3) (find 33rd boss of employee 55). Build upup: up[5][0] = 4, up[5][1] = 3, up[5][2] = 1. Now jump: k=3=112k = 3 = 11_2 (bits 00, 11 set). Jump 21=22^1 = 2: v = up[5][1] = 3. Jump 20=12^0 = 1: v = up[3][0] = 2. Answer: 22.

You reached the answer in 22 jumps instead of 33 single steps. For large trees, the savings are dramatic.