Latin Squares
시간 제한0.5초메모리 제한2048 MB
행과 열을 교환하는 연산 순서가 주어질 때, 그 연산 전체를 적용해도 변하지 않는 라틴 방진이 존재하는지 판정하고, 존재하면 그러한 방진 하나를 출력한다.
문제
A Latin square of size is an matrix where each entry is an integer between and , and no two entries in the same row or column are equal.
Alice has a Latin square of size , and she applied a list of transformations to it. Each transformation swaps either two distinct rows or two distinct columns of the matrix. The transformations are applied sequentially: the first transformation is applied to the original matrix, the second to the result of the first transformation, and so on. The final result is, of course, an matrix.
Alice will not reveal any matrix to you, only the list of transformations. Your task is to determine whether there exists an original matrix that, after applying all transformations, is identical to the final matrix. If such a matrix exists, you must also provide a possible content for this original matrix.
입력
The first line contains two integers () and (), indicating respectively the size of the Latin square and the number of transformations.
The next lines describe the transformations, in the order they are applied, one transformation per line. Each of these lines contains a character and two integers and ( and ). The character is either the uppercase letter “R” or the uppercase letter “C” indicating respectively that rows and , or columns and are swapped.
출력
If there exists an original matrix that, after applying all transformations, is identical to the final matrix, output lines with integers each, representing any such matrix.
If no such matrix exists, output a line with the character “*” (asterisk) instead.