Editorial for CCC '24 S4 - Painting Roads
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
We can model the intersections and roads as an undirected graph. In this subtask, the roads connecting intersection
First, observe that Alanna must paint at least
Because we're guaranteed a Hamiltonian path
Subtask 2
In this subtask, the graph is connected and has an equal number of edges and nodes. In other words, the graph forms a pseudotree. In particular, there is exactly one simple cycle in the graph.
Just like in subtask
This cycle may be found by a carefully implemented graph-traversal algorithm like BFS or DFS.
Subtask 3
In this subtask, the graph is no longer guaranteed to be connected. The condition of no road belonging to two or more simple cycles can be rephrased as each connected component of the graph being a cactus graph.
By generalizing the ideas from subtasks
Let's consider choosing an arbitrary spanning forest of the graph to colour with red or blue. For each grey edge
An arbitrary spanning forest can be found with algorithms like BFS or DFS. To determine the correct colours, we cannot afford to spend
Subtask 4
The solution from subtask
Guided by these intuitions, let's think about what conditions on the spanning forest would be nice to have. Let's focus on a single connected component, where the coloured edges form a tree, and let's root the tree arbitrarily. Consider a grey edge
Intuitively, most constraints are from types (1) and (2). It is actually possible to satisfy all type (1) and (2) constraints by colouring each edge by the parity of its depth in the tree, but this fails to satisfy any type (3) constraints. This motivates us to consider: Is there a spanning forest where we end up with no type (3) constraints?
In other words, we want a spanning forest with no cross edges: edges that do not connect an ancestor with a descendent. For undirected graphs, the DFS tree (tree formed by a depth-first traversal) has exactly this property we're looking for. This results in a very short solution to the full problem: For each connected component, take a DFS tree and colour the tree edges red or blue based on the parity of its depth.
Comments