Editorial for COCI '21 Contest 5 #3 Fliper
Submitting an official solution before solving the problem yourself is a bannable offence.
Clearly, a necessary condition is that the number of bounces in each cycle is divisible by . We'll show that this is also sufficient.
First off, we need to determine all the cycles in which the ball could get stuck. We can split each obstacle into two pieces, so that each piece corresponds to one side of the obstacle. Then for each -coordinate, we make a list of all pieces having that -coordinate and sort it by their -coordinate. After that, we connect pairs of adjacent pieces one after another to mark them as being in the same cycle. The pieces at the ends of the list are connected with a special label representing that they are not part of any cycle, but rather that they are connected to infinity. We do a similar thing for the -coordinate. The described joining can be maintained in a disjoint set union structure.
In this way, we've determined for each piece of an obstacle in which cycle it belongs to (or that it doesn't belong to any cycle, but that it's connected to infinity). Now we make a graph: each of the found cycles will be a node in the graph (and there is a dummy node representing infinity), and for each obstacle, we add an edge between the nodes corresponding to the pieces the obstacle is made from. It is possible that an edge connects some node to itself.
Notice that the number of bounces in a cycle now represents the degree of a node. The problem can now be rephrased as follows: given a connected graph where the degree of each node is divisible by (except maybe for the dummy node), color the edges of the graph in four colors so that each node (except the dummy node) is connected to an equal number of edges of each color.
The problem can now be solved using Eulerian tours. Note that if every node in a connected graph has an even degree, we can start an Eulerian tour from some node and color the edges alternately in two colors. In that way, we make it so that each node except for the starting node (which also happens to be the ending node) has an equal number of edges of each of the two colors. This might also hold for the starting node, but only if the number of edges in the graph is even.
For the graph in the problem, we know the degree of each node except the dummy node is divisible by . The handshaking lemma guarantees that the degree of the dummy node is also even. Since the degree of each node is even and the graph is connected, we can start an Eulerian tour from the dummy node. In that way, we color the edges in two colors so that each node except maybe the dummy node has an equal number of edges of these two colors. The problem requires us to find a coloring using four colors, so we'll find Eulerian tours again, but separately on the edges of one color and then the edges of the other. Namely, if we look at only the edges of one color, the degree of each node (except maybe the dummy node) is divisible by . The graph will not necessarily be connected, but this is not a problem because the handshaking lemma guarantees that each component not connected to the dummy node has an even number of edges.
Comments