Codeforces Round 1077 (Div. 1)
G
Many Cartesian Trees
Codeforces
3500
原题
← F2→
翻译中…

Consider an array $a$ consisting of $n$ distinct integers. The Cartesian Tree of $a$ is defined as a unique binary tree$^{\text{∗}}$ that satisfies the following conditions:

翻译中…
  • The tree consists of $n$ vertices labeled from $1$ to $n$;
  • For all pairs of vertices $(i,j)$ such that $i$ is a parent$^{\text{†}}$ of $j$, $a_i \gt a_j$;
  • For any vertex $i$, all vertices in the left subtree$^{\text{‡}}$ of $i$ have labels less than $i$;
  • For any vertex $i$, all vertices in the right subtree of $i$ have labels greater than $i$.
翻译中…

Denote $r(a)$ as the root of the Cartesian Tree, and $f_a(i)$ as the parent of $i$ in the Cartesian Tree.

翻译中…

Define $E_a=\{(i,f_a(i)) \mid 1 \le i \le n, i \ne r(a)\}$.

翻译中…

Consider a permutation$^{\text{§}}$ $q$ of length $n$. We define a series of arrays $p_1,p_2,\ldots,p_n$ as follows:

翻译中…
  • $p_1=q$;
  • For each $2 \le i \le n$, $p_i$ is obtained by incrementing the minimum element in $p_{i-1}$ by $n$. It can be shown that all elements in $p_i$ are distinct.
翻译中…

You are now given $n$ and $S=\bigcup\limits_{i=1}^n E_{p_i}$. To minimize input, $S$ will be given in the form of a $n \times n$ binary matrix $s$. Denote $s_{i,j}$ as the $j$-th character of the $i$-th row. $s_{i,j}=1$ if and only if $(i,j) \in S$.

翻译中…

Please find any permutation $q$ such that $S$ can be obtained by the process above. It is guaranteed that the tests are generated to ensure a valid $q$ always exists.

翻译中…

$^{\text{∗}}$A binary tree is a rooted tree, in which each node has no more than $2$ children, referred to as the left child and the right child.$^{\text{†}}$The parent of vertex $v$ is the first vertex on the simple path from $v$ to the root. The root has no parent. $^{\text{‡}}$A subtree of vertex $v$ is the subgraph of $v$, all its descendants, and all the edges between them. $^{\text{§}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test contains an integer $n$ ($2 \le n \le 3000$) — the length of $q$ that you need to find.

翻译中…

The $i$-th of the next $n$ lines contains a binary string of length $n$, representing the $i$-th row of $s$. It is guaranteed that $s_{i,i} = 0$ for all $1 \le i \le n$.

翻译中…

It is guaranteed that the tests are generated to ensure a valid $q$ always exists.

翻译中…

It is guaranteed that the sum of $n^2$ over all test cases does not exceed $9 \cdot 10^6$.

翻译中…

Output

翻译中…

For each test case, output $n$ integers $q_1,q_2,\ldots,q_n$, representing the permutation $q$ you found.

翻译中…

Note

翻译中…

image

翻译中…

样例

Input #1
3
2
01
10
3
011
100
010
4
0101
0010
1001
0110
Output #1
1 2
2 1 3
3 1 2 4
InputCopy OutputCopy
题目信息
题号
2187G
时间限制
2 s
内存限制
256 MB
标签
贪心
排序
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。