Graph Theory37 sections · 1633 units
Open in Course

Problem - Distance Queries - Read Statement

(CSES 1135)

You need to answer distance queries between pairs of nodes in a tree. The distance is the number of edges on the path connecting them. The formula:

dist(u, v) = depth[u] + depth[v] - 2 * depth[lca(u, v)]

Preprocess the tree with binary lifting to answer LCA queries in O(log⁡n)O(\log n) time, then compute distances using the formula. Try implementing this yourself. You already know binary lifting from the previous section.