Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Precompute Depths and LCA

(Setup for fast queries)

Run DFS to compute depth[v] for each node. Build the binary lifting table or Euler tour + sparse table for fast LCA queries. With these precomputed structures, each distance query becomes a simple formula: depth[u] + depth[v] - 22 * depth[lca(u, v)]. The preprocessing cost (O(nlogn))O(n \log n)) is paid once, then you answer many queries efficiently.

This is a classic space-time tradeoff: spend more memory and preprocessing time to get faster queries. The same pattern appears in many tree problems: precompute structural information (depths, ancestors, subtree sizes), then use it to answer queries quickly.