Omkar and Mosaic
Contest: Codeforces Round 749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1)
Difficulty rating: 3500
Tags: combinatorics, constructive algorithms, math

Omkar is creating a mosaic using colored square tiles, which he places in an $$$n \times n$$$ grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells.

A completed mosaic will be a mastapeece if and only if each tile is adjacent to exactly $$$2$$$ tiles of the same color ($$$2$$$ tiles are adjacent if they share a side.) Omkar wants to fill the rest of the tiles so that the mosaic becomes a mastapeece. Now he is wondering, is the way to do this unique, and if it is, what is it?

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$).

Then follow $$$n$$$ lines with $$$n$$$ characters in each line. The $$$i$$$-th character in the $$$j$$$-th line corresponds to the cell in row $$$i$$$ and column $$$j$$$ of the grid, and will be $$$S$$$ if Omkar has placed a sinoper tile in this cell, $$$G$$$ if Omkar has placed a glaucous tile, $$$.$$$ if it's empty.

Output format:
On the first line, print UNIQUE if there is a unique way to get a mastapeece, NONE if Omkar cannot create any, and MULTIPLE if there is more than one way to do so. All letters must be uppercase.

If you print UNIQUE, then print $$$n$$$ additional lines with $$$n$$$ characters in each line, such that the $$$i$$$-th character in the $$$j^{\text{th}}$$$ line is $$$S$$$ if the tile in row $$$i$$$ and column $$$j$$$ of the mastapeece is sinoper, and $$$G$$$ if it is glaucous.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first test case, Omkar can make the mastapeeces

SSSS

SGGS

SGGS

SSSS

and

SSGG

SSGG

GGSS

GGSS.

For the second test case, it can be proven that it is impossible for Omkar to add tiles to create a mastapeece.

For the third case, it can be proven that the given mastapeece is the only mastapeece Omkar can create by adding tiles.

For the fourth test case, it's clearly impossible for the only tile in any mosaic Omkar creates to be adjacent to two tiles of the same color, as it will be adjacent to $$$0$$$ tiles total.

Example input:
4
S...
..G.
....
...S

Example output:
MULTIPLE

Example input:
6
S.....
....G.
..S...
.....S
....G.
G.....

Example output:
NONE

Editorial:
The first main observation to make is that the possible mastapeeces don't just have square "loops" of the same color. A counterexample to this is shown below:
Instead, observe that, in a mastapeece:  a) The two cells adjacent to corner cells must be the same color as the corner.  b) Any cell not on the border must be adjacent to two sinoper tiles and two glaucous tiles  If we then start at two cells adjacent to some corner and keep applying b) to cells on the long diagonal with the corner, we find that the long diagonals starting at the adjacent cells must be identical and tiled alternately with glaucous and sinoper tiles, like so:
From here, it's pretty easy to figure out how to implement the problem.
Some examples of what mastapeeces look like are shown below: