#P14616. [2019 KAIST RUN Fall] Gosu 2

[2019 KAIST RUN Fall] Gosu 2

题目描述

Ho is an expert in martial arts called Taebo\textit{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\textit{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\textbf{standard ranking chain} and assign other students with respect to such a chain. A standard ranking chain\textbf{standard ranking chain} of length KK, is a sequence of KK different students S1,S2,,SkS_1, S_2, \ldots, S_k such that SiS_i beats SjS_j if and only if i<ji < j. In other words, S1S_1 can beat all other students in the chain, S2S_2 can beat all other students in the chain except S1S_1, S3S_3 can beat all other students in the chain except S1,S2S_1, S_2, and so on, and SkS_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+log2(N)1 + \lfloor \log_2(N) \rfloor, where log2(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+log2(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 sis_i consisting of W\texttt{W}, L\texttt{L}, and -\texttt{-}, is given. Let's denote the jj-th character of sis_i as si,js_{i,j}. si,js_{i,j} is given as follows:

  • si,j=s_{i,j}= -\texttt{-}, if i=ji=j.
  • si,j=s_{i,j}= W\texttt{W}, if student ii won student jj.
  • si,j=s_{i,j}= L\texttt{L}, if student jj won student ii.
  • 1T2500001 \le T \le 250\,000
  • 1N5121 \le N \le 512
  • The sum of N2N^2 for all test cases does not exceed 25000002\,500\,000.
  • si,i=s_{i, i} = -\texttt{-} (1iN1 \le i \le N)
  • If iji \neq j, then si,j=s_{i, j}= W\texttt{W} or si,j=s_{i, j}= L\texttt{L}. (1iN1 \le i \le N)
  • If si,j=s_{i, j} = W\texttt{W}, then sj,i=s_{j, i} = L\texttt{L}. (1i, jN1 \le i,\ j \le N)
  • If si,j=s_{i, j} = L\texttt{L}, then sj,i=s_{j, i} = W\texttt{W}. (1i, jN1 \le i,\ j \le N)

输出格式

For each test case, print exactly 1+log2(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.

5
1
-
2
-W
L3
-LW
W-L
LW4
-WLW
L-WL
WL-W
LWL5
-WLLW
L-LLW
WW-LL
WWW-W
LLWL
1
1 2
3 2
3 1 4
4 3 1