Graph Theory37 sections · 1633 units
Open in Course

CSES 1667 Message Route - Path Reconstruction Idea

Breadcrumbs

BFS computes distances, but it does not save the path by default. To reconstruct the path, track where each node came from. Use a parent array: int parent[N]. When you visit node vv from node uu, set parent[v] = u. This creates a trail of breadcrumbs from the target back to the source. After BFS finishes, if you want the path to node tt, start at tt and follow the parent pointers: t → parent[t] → parent[parent[t]] → ... until you reach the source. Reverse this sequence to get the path from source to target.