eolymp
bolt
Try our new interface for solving problems
Problems

Connecting Islands

Connecting Islands

The civil war in the Bytelandian ocean has ended. The two Bytelandian islands have decided to unite forming the United Kingdom of Bytelandia. One of the rst concerns of the newly chosen government is transportation. Prior to the war each of the \textbf{2} islands had a perfect network of transportation. On any given island, every city was connected by a train to every other city. Connections are bidirectional. During the war, some train lines were disrupted and thus, the trains on these lines were suspended. On any island, no more than \textbf{15} trains were suspended. In addition to rerunning the disrupted trains, the government has to form inter-island ferry connections. The ferries will connect every city \textbf{X} to every city \textbf{Y}, given that \textbf{X} and \textbf{Y} are not on the same island. Commuting between cities on the same island will only be via trains. In Bytelandia, everything is binary, including train and ferry ticket prices. A ticket costs either \textbf{0} or \textbf{1} units of currency. The government decided not to change any ticket prices for trains that were not disrupted during the war. It will assign ticket costs to the trains suspended during the war and to all ferry lines. Priding themselves in being strong mathematicians, Bytelandians have decided to design the ticket system such that for any \textbf{3} distinct cities \textbf{X}, \textbf{Y} and \textbf{Z}, \textbf{C(X, Y) + C(Y, Z) }≥\textbf{ C(X , Z)} where \textbf{C(X, Y)} is the ticket price of getting from \textbf{X} to \textbf{Y} (by train if they belong to the same island or by ferry otherwise). You are given a description of the Bytelandian islands, your goal is to gure out a costs assignment if it is possible. \InputFile Your program will be tested on one or more test cases. The ашrst line of the input will be a single integer \textbf{T}, the number of test cases (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}). Followed by the test cases, each test case is the description of both islands (one after the other). The description of one island starts with one line containing one integer \textbf{C_k} represents the number of cities in this island, followed by \textbf{C_k} lines each one contains \textbf{C_k} characters (\textbf{1} ≤ \textbf{C_k} ≤ \textbf{100}). Each character is '\textbf{0}', '\textbf{1}' or '\textbf{x}'. The \textbf{j}th character of the \textbf{i}th line is the price of the train ticket from city \textbf{i} to city \textbf{j} on that island. A price '\textbf{x}' means that it is one of the suspended train lines. Each \textbf{C_k} by \textbf{C_k} table is guaranteed to be symmetric (\textbf{table\[i\]\[j\] = table\[j\]\[i\] }for each different \textbf{i} and \textbf{j}), and each table will have a diagonal of '\textbf{0}'s (\textbf{table\[i\]\[i\] = }'\textbf{0}' for each dierent \textbf{i}), also each table will contain no more than \textbf{30} '\textbf{x}'s (\textbf{15} different trains, because each train will be mentioned twice in the table). \textit{Note that the input might contain }\textit{\textbf{3}}\textit{ different cities in the same islands and the trains connecting them are not suspended, and these }\textit{\textbf{3}}\textit{ cities don't satisfy the described condition above.} \OutputFile For each test case, if there is no way to do the costs assignment satisfying the conditions described above, output on a single line \textbf{NO} (without the quotes). Otherwise, print \textbf{YES} (without the quotes) on the first line followed by \textbf{C_1+C_\{2 \}}lines each one contains \textbf{C_1+C_2} characters (\textbf{C_1} is the number of cities in the first island, and \textbf{C_2} is the number of cities in the second island). The \textbf{j}th character on the \textbf{i}th line is the cost of getting from city \textbf{i} to city \textbf{j} (by train if they belong to the same island or by ferry otherwise). In the output, the cities on the first island are numbered from \textbf{1} to \textbf{C_1}(inclusive) while the cities in the second island are numbered from \textbf{C_1+1} to \textbf{C_1+C_2} (inclusive). If there is more than one correct solution, print anyone of them. \textit{Note that you can't change the costs given in the input, and the output table must be symmetric.}
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
3
011
10x
1x0
2
01
10
2
0x
x0
4
010x
1010
0100
x000
Output example #1
YES
01101
10011
10011
01101
11110
NO
Source Arab Collegiate Programming Contest 2012