Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Constructor Preprocessing

(Build table once)

The constructor is called once with the tree structure (parent array). This is where you build the upup table. Compute logn\log n (around 1616 for n=50,000n = 50,000). Allocate up[n][log n]. Fill it column by column as before. Time: O(nlogn)O(n \log n). This runs only once, so it is fine even if nn is large.

Each query then reads from the precomputed table. The amortized cost per operation is low because the preprocessing dominates.