Editorial for COI '06 #2 Policija
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
A single depth-first search collects all the information needed to answer the queries. While searching, we store the following numbers for each vertex:
- Discovery time – discrete time index at which we started processing the vertex
- Finishing time – time index when we finished processing the vertex
- Depth – the depth of the vertex in the DFS tree
- lowlink – the vertex with the least discovery time, that is reachable from the current vertex or from its descendants (via a single back edge)
These numbers allow us to construct two useful functions:
- – is vertex in the subtree rooted at
- – when is in vertex 's subtree, find the immediate child of such that is a descendant of that child (in other words, if has multiple children, find which of those subtrees is in)
With some case analysis, it is possible to answer the queries using the described functions.
For an in-depth discussion of depth-first search and its applications, see "Introduction to Algorithms" by Cormen et al. This problem is closely related to finding articulation points and bridges in a graph.
Comments