公式

G - 오fill 解説 by sorohue


Focus on whether there is a path at the tile’s boundary. Furthermore, by examining the shapes of the available tiles, we can see that in one direction (either horizontal or vertical), the presence of a path remains unchanged, while in the other direction, it changes.

Therefore, we can reformulate the problem by assigning a horizontal or vertical direction to each empty space, and ask for the number of ways to arrange the tiles such that every block of empty spaces connected in a row is properly connected to the tiles at its ends.

Consider a graph where each empty slot is an edge and each block is a vertex. From the tiles adjacent to both ends of each block, we can determine the parity of the horizontal tiles that must be placed in each block. At this point, we can further reduce the problem to finding the number of ways to assign \(0\) or \(1\) to each vertex, then select edges appropriately so that XORing the \(1\)s at the endpoints of each selected edge results in all vertices becoming \(0\).

Each component is independent. Let us consider the case of a single component consisting of \(V\) vertices and \(E\) edges. When an edge is selected, \(1\) is added to or subtracted from the two vertices. Therefore, if the sum of the values assigned to the vertices is odd, it is impossible to make all vertices \(0\).

Consider any spanning tree of the graph. If the sum of the values assigned to the vertices is even, we can obtain the unique way to set all vertices to \(0\) using only the edges of that spanning tree by determining whether to select each edge in reverse order starting from the leaves of the spanning tree.

We can see that for the remaining edges not included in the spanning tree, no matter how we choose them, there is a unique way to resolve the situation using only the edges of the spanning tree. Therefore, the number of ways to fill a component with tiles is \(2^{E-(V-1)}\).

Thus, letting \(V\) be the number of blocks, \(E\) be the number of empty spaces, and \(F\) be the number of components, the answer is \(2^{E-V+F}\). Since all values required for the calculation can be easily obtained in \({\cal O}(NM)\), the entire problem can also be solved in \({\cal O}(NM)\).

投稿日時:
最終更新: