Latin Squares

시간 제한0.5초메모리 제한2048 MB

요약
행과 열을 교환하는 연산 순서가 주어질 때, 그 연산 전체를 적용해도 변하지 않는 라틴 방진이 존재하는지 판정하고, 존재하면 그러한 방진 하나를 출력한다.
난이도

보통10점 중 7점

유형
수학, 조합론, 구현, 시뮬레이션
정답자
아직 제출이 없습니다

문제

A Latin square of size NN is an N×NN \times N matrix where each entry is an integer between 11 and NN, and no two entries in the same row or column are equal.

Alice has a Latin square of size NN, 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 N×NN \times N 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 NN (2≤N≤5002 ≤ N ≤ 500) and TT (1≤T≤1051 ≤ T ≤ 10^5), indicating respectively the size of the Latin square and the number of transformations.

The next TT lines describe the transformations, in the order they are applied, one transformation per line. Each of these lines contains a character XX and two integers II and JJ (1≤I,J≤N1 ≤ I, J ≤ N and I≠JI \ne J). The character XX is either the uppercase letter “R” or the uppercase letter “C” indicating respectively that rows II and JJ, or columns II and JJ are swapped.

출력

If there exists an original matrix that, after applying all transformations, is identical to the final matrix, output NN lines with NN integers each, representing any such matrix.

If no such matrix exists, output a line with the character “*” (asterisk) instead.

예제2

  1. 예제 1

    입력
    4 4
    R 1 2
    C 2 1
    R 3 4
    C 3 4
    
    예상 출력
    1 2 3 4
    2 1 4 3
    3 4 1 2
    4 3 2 1
    
  2. 예제 2

    입력
    4 3
    R 1 2
    R 1 2
    R 2 1
    
    예상 출력
    *