Gosu 2

N명의 학생 사이 승패 결과가 주어질 때, 앞선 학생이 뒤의 모든 학생을 이기는 1 + floor(log2 N) 길이의 사슬을 찾는다.

어려움9그래프그리디분할 정복재귀아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

Ho is an expert in martial arts called Taebo. She runs a Taebo school, and there are NN students in her school. To increase the inner competition inside the Taebo school, she is going to make a Taebo ranking website which assigns all students to a certain rank. To find a suitable rank, Ho made all N(N1)/2N(N-1)/2 pairs of students do a Taebo matchup with each other. In a Taebo matchup, exactly one person wins the match, and another person loses the match. The outcome of Taebo matchups may not be very simple: For example, there might be a case that student A beats B, B beats C, and C beats A. Such situation would make the ranking assignment pretty complicated as there is no definite winner from those three students.

To overcome the issue, Ho will find a standard ranking chain and assign other students with respect to such a chain. A standard ranking chain of length KK, is a sequence of KK different students S_1, S_2, , S_kS\_1,\ S\_2,\ \cdots,\ S\_k such that S_iS\_i beats S_jS\_j if and only if i<ji < j. In other words, S_1S\_1 can beat all other students in the chain, S_2S\_2 can beat all other students in the chain except S_1S\_1, S_3S\_3 can beat all other students in the chain except S_1,S_2S\_1, S\_2, and so on, and S_kS\_k can beat no other student in the chain. Ho's website will assign other students based on such a chain, which will make the assignment easier.

Ho is not only an expert in Taebo, but she is a math genius too. Ho knows, that for any Taebo matchup, she can find the standard ranking chain of length 1+log_2(N)1 + \lfloor \log\_2(N) \rfloor, where log_2(N)\log\_2(N) is a base 2 logarithm. In other words, for any k1k \geq 1 such that 2k1N2^{k-1} \le N, Ho can find a standard ranking chain of such a length. 

While Ho is very good at computer programming too, she is a little bit lazy, therefore she delegates her work to you. You should find a standard ranking chain of length exactly 1+log_2(N)1 + \lfloor \log\_2(N) \rfloor.

입력

In the first line, the number of test cases TT is given. For each test case, the following instances are given:

In the first line, the number of students NN is given.

In the ii-th line of the next NN lines, a string of NN characters, s_is\_i, consisting of W, L, and - is given. Let's denote the jj-th character of s_is\_i as s_i, js\_{i,\ j}. s_i, js\_{i,\ j} is given as follows:

  • s_i, j=s\_{i,\ j}= -, if i=ji=j.
  • s_i, j=s\_{i,\ j}= W, if student ii won student jj.
  • s_i, j=s\_{i,\ j}= L, if student jj won student ii.

 

  • 1T250,0001 \le T \le 250\\,000
  • 1N5121 \le N \le 512
  • The sum of N2N^2 for all test cases does not exceed 2,500,0002\\,500\\,000.
  • s_i,i=s\_{i, i} = - (1iN1 \le i \le N)
  • If iji \neq j, then s_i,j=s\_{i, j}= W or s_i,j=s\_{i, j}= L. (1iN1 \le i \le N)
  • If s_i,j=s\_{i, j} = W, then s_j,i=s\_{j, i} = L. (1i, jN1 \le i,\ j \le N)
  • If s_i,j=s\_{i, j} = L, then s_j,i=s\_{j, i} =  W. (1i, jN1 \le i,\ j \le N)

출력

For each test case, print exactly 1+log_2(N)1 + \lfloor \log\_2(N) \rfloor integers in a single line, denoting the students in a standard ranking chain in the order of their skills. It can be proved that such a chain exists for every possible input.