Seollal
Contest: Hello 2020
Difficulty rating: 3300
Tags: graphs

It is only a few days until Seollal (Korean Lunar New Year), and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek.

The garden can be represented by a $$$n \times m$$$ grid of unit cells. Some (possibly zero) cells are blocked by rocks, and the remaining cells are free. Two cells are neighbors if they share an edge. Each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction.

Since the garden is represented as a grid, we can classify the cells in the garden as either "black" or "white". The top-left cell is black, and two cells which are neighbors must be different colors. Cell indices are 1-based, so the top-left corner of the garden is cell $$$(1, 1)$$$.

Jaehyun wants to turn his garden into a maze by placing some walls between two cells. Walls can only be placed between neighboring cells. If the wall is placed between two neighboring cells $$$a$$$ and $$$b$$$, then the two cells $$$a$$$ and $$$b$$$ are not neighboring from that point. One can walk directly between two neighboring cells if and only if there is no wall directly between them.

A maze must have the following property. For each pair of free cells in the maze, there must be exactly one simple path between them. A simple path between cells $$$a$$$ and $$$b$$$ is a sequence of free cells in which the first cell is $$$a$$$, the last cell is $$$b$$$, all cells are distinct, and any two consecutive cells are neighbors which are not directly blocked by a wall.

At first, kids will gather in cell $$$(1, 1)$$$, and start the hide-and-seek game. A kid can hide in a cell if and only if that cell is free, it is not $$$(1, 1)$$$, and has exactly one free neighbor. Jaehyun planted roses in the black cells, so it's dangerous if the kids hide there. So Jaehyun wants to create a maze where the kids can only hide in white cells.

You are given the map of the garden as input. Your task is to help Jaehyun create a maze.

Input format:
Your program will be judged in multiple test cases.

The first line contains the number of test cases $$$t$$$. ($$$1 \le t \le 100$$$). Afterward, $$$t$$$ test cases with the described format will be given.

The first line of a test contains two integers $$$n, m$$$ ($$$2 \le n, m \le 20$$$), the size of the grid.

In the next $$$n$$$ line of a test contains a string of length $$$m$$$, consisting of the following characters (without any whitespace):

- O: A free cell.
- X: A rock.

It is guaranteed that the first cell (cell $$$(1, 1)$$$) is free, and every free cell is reachable from $$$(1, 1)$$$.

If $$$t \geq 2$$$ is satisfied, then the size of the grid will satisfy $$$n \le 10, m \le 10$$$. In other words, if any grid with size $$$n > 10$$$ or $$$m > 10$$$ is given as an input, then it will be the only input on the test case ($$$t = 1$$$).

Output format:
For each test case, print the following:

If there are no possible mazes, print a single line NO.

Otherwise, print a single line YES, followed by a grid of size $$$(2n-1) \times (2m-1)$$$ denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base.

- For all $$$1 \le i \le n, 1 \le j \le m$$$, if the cell $$$(i, j)$$$ is free cell, print 'O' in the cell $$$(2i-1, 2j-1)$$$. Otherwise, print 'X' in the cell $$$(2i-1, 2j-1)$$$.
- For all $$$1 \le i \le n, 1 \le j \le m-1$$$, if the neighboring cell $$$(i, j), (i, j+1)$$$ have wall blocking it, print ' ' in the cell $$$(2i-1, 2j)$$$. Otherwise, print any printable character except spaces in the cell $$$(2i-1, 2j)$$$. A printable character has an ASCII code in range $$$[32, 126]$$$: This includes spaces and alphanumeric characters.
- For all $$$1 \le i \le n-1, 1 \le j \le m$$$, if the neighboring cell $$$(i, j), (i+1, j)$$$ have wall blocking it, print ' ' in the cell $$$(2i, 2j-1)$$$. Otherwise, print any printable character except spaces in the cell $$$(2i, 2j-1)$$$
- For all $$$1 \le i \le n-1, 1 \le j \le m-1$$$, print any printable character in the cell $$$(2i, 2j)$$$.

Please, be careful about trailing newline characters or spaces. Each row of the grid should contain exactly $$$2m-1$$$ characters, and rows should be separated by a newline character. Trailing spaces must not be omitted in a row.

Constraints:
Time limit: 3.0 s
Memory limit: 1024.0 MB

Example input:
4
2 2
OO
OO
3 3
OOO
XOO
OOO
4 4
OOOX
XOOX
OOXO
OOOO
5 6
OOOOOO
OOOOOO
OOOOOO
OOOOOO
OOOOOO

Example output:
YES
OOO
  O
OOO
NO
YES
OOOOO X
  O O  
X O O X
  O    
OOO X O
O O   O
O OOOOO
YES
OOOOOOOOOOO
  O   O   O
OOO OOO OOO
O   O   O  
OOO OOO OOO
  O   O   O
OOO OOO OOO
O   O   O  
OOO OOO OOO

Editorial:
The problem asks us to find a spanning tree on a grid graph, where some vertices are not allowed to be leaves. Let's denote such set of vertices as $$$S$$$. Note that, per the problem statement, this set is independent. Thus, there exists no edge connecting two vertices in $$$S$$$. Now, we present the following theorem.
Theorem. We can find a desired spanning tree if and only if there exists an acyclic edge subset where every vertex in $$$S$$$ has a degree exactly 2.
Proof.
$$$\leftarrow$$$: Since it is an acyclic edge subset, we can obviously expand it to a spanning tree by arbitrarily adding edges to connect different connected components. Vertices with degree at least 2 are not leaves.
$$$\rightarrow$$$: As noted above, $$$S$$$ is independent, and it has degree at least 2 in the desired spanning tree. Thus, we can pick any two incident edges for each vertex in $$$S$$$.
Now, all that is left is to find such an acyclic edge subset. In general, as $$$S$$$ is independent, we can pick two arbitrary incident edges, but this does not guarantee that the subset is acyclic. One can try to use a non-polynomial solution strategy, such as storing connection profiles in a DP, but the input is a bit too large for that.
Our solution is polynomial, and we will use a stronger tool that is known as matroid intersection. From now on, we will assume that the reader has familiarity with matroids as well as how to compute the intersection of two matroids.
Let's relax the degree condition to at most 2 instead of exactly 2. Now, the acyclic condition corresponds to a graphic matroid, and the degree condition corresponds to a choice matroid. By using Edmond's algorithm, we can find an independent set of maximum size. The exact degree condition is satisfied if and only if the maximum independent set has size $$$2|S|$$$, so we can know whether such an edge set exists, and if it exists we can also get a certificate which is then used to print the answer.
In our matroid, we can implement Edmond's matroid intersection algorithm in $$$O(E^3)$$$ time where $$$E$$$ is the size of the ground set. As $$$E$$$ is about $$$2nm$$$, the final time complexity is $$$O(n^3m^3)$$$ with low constants, just like any other augmenting-path algorithms.