Graph Theory37 sections · 1633 units
Open in Course

Problem - Counting Paths - Read Statement

(CSES 1136)

This problem extends distance queries. You need to count how many marked nodes appear on the path from uu to vv. The path from uu to vv goes through their LCA. Split the problem: count marked nodes from uu to lca(u,v)lca(u, v), plus marked nodes from vv to lca(u,v)lca(u, v).

Subtract the LCA if it was counted twice. Think about how to preprocess the tree to answer "how many marked nodes are there from node xx up to the root?" in constant time.