#P12553. [UOI 2024] Lady's Gift

[UOI 2024] Lady's Gift

题目描述

On her birthday, Lady received a gift --- a network. The network contains nn nodes, numbered with integers from 11 to nn. Each node is assigned a specific letter, denoted as sis_i for the node with number ii.

There are one-way connections between some pairs of nodes. Each node has exactly one outgoing one-way connection. Let the connection for the node with number ii lead to the node with number xix_i. Note that xix_i can be equal to ii --- in this case, it is considered that the connection from the node with number ii leads to the same node.

Let pi,0=ip_{i,0} = i and pi,k=pxi,k1p_{i,k} = p_{x_i,k-1}. Thus, pi,kp_{i,k} is the number of the node where a chip will end up if it is placed in the node with number ii and moved along the connection from the current node kk times.

Lady created a matrix aa of size n×(3n)n\times (3\cdot n), where ai,j=spi,j1a_{i,j} = s_{p_{i,j-1}}. Thus, the ii-th row of the matrix aa is a sequence of 3n3\cdot n letters, where the first letter equals sis_i, the second letter equals sxis_{x_i}, the third equals sxxis_{x_{x_i}}, and so on.

Lady informed some rows of the matrix aa and asks you to construct any network that corresponds to the known rows of the matrix aa.

输入格式

The first line contains a single integer nn (1n2103)(1 \leq n \leq 2\cdot{10}^3) --- the number of nodes in the network.

The next nn lines describe the matrix aa. Each of these lines contains 3n3\cdot n lowercase Latin letters, representing the corresponding row of matrix aa, or a single symbol ?\tt{?} if Lady did not inform the corresponding row.

It is guaranteed that there exists at least one network that satisfies the given conditions.

输出格式

In the first line, output a string ss consisting of nn lowercase Latin letters --- the letters written on the nodes of the network.

In the second line, output nn integers x1,x2,,xnx_1, x_2, \ldots, x_n (1xin)(1 \leq x_i \leq n) --- the numbers of the nodes to which the connections from the corresponding nodes lead.

The matrix corresponding to this network must be exactly equal to the matrix aa in all rows that Lady informed.

If there are multiple correct answers, you are allowed to output any of them.

4
abaaaaaaaaaa
baaaaaaaaaaa
aaaaaaaaaaaa
cccccccccccc
abac
2 3 3 4
3
axaxaxaxa
xxxxxxxxx
?
axx
3 2 1

提示

In the first example, x1=2x_1 = 2 and x2=3x_2 = 3, so p1,0=1,p1,1=2,p1,2=3p_{1,0}=1, p_{1,1}=2, p_{1,2}=3. Since x3=3x_3=3, all p1,kp_{1,k} for k3k\geq3 are also equal to 33. Accordingly, the second letter of the first row equals s2s_2, and the third equals s3s_3.

Below are the images of networks from the examples. The numbers in the corners indicate the node numbers, the letters indicate the values written on the corresponding nodes, and the arrows indicate the one-way connections.

Network from the first example

Network from the second example

Scoring

Let qq be the number of rows that Lady did not inform.

We call a network a set of pairs and unit nodes if the network can be divided into nodes, from which the connection leads to itself (i.e., xv=vx_v = v), and pairs of nodes (a,b)(a,b) such that xa=bx_a=b and xb=ax_b=a.

We call a network a set of stars if the network can be divided into separate stars, each of which consists of a main node vv, from which the connection leads to itself (i.e., xv=vx_v = v), and a set of secondary nodes y={y1,y2,,yk}y=\{y_1, y_2, \ldots, y_k\} such that xyi=vx_{y_i}=v for 1ik1 \leq i \leq k. Note that the stars in the network may have different sizes and consist only of one main node.

We call a network a tree with the root at node vv if from node vv the connection leads to itself (i.e., xv=vx_v = v), and for each other node it is possible to reach node vv using the network connections (i.e., for each 1in1 \leq i \leq n there is such kk that pi,k=vp_{i,k}=v).

We call a network a cycle if starting from any node it is possible to reach any other node using the network connections (i.e., for all 1i,jn1 \leq i,j \leq n there is such kk that pi,k=jp_{i,k}=j).

  • (1010 points): n5n \leq 5, q=0q = 0;
  • (66 points): n300n \leq 300, q=0q = 0, xxi=ix_{x_i}=i for 1in1\le i\le n (the network is a set of pairs and unit nodes);
  • (66 points): n300n \leq 300, q=0q = 0, xxi=xix_{x_i}=x_i for 1in1\le i\le n (the network is a set of stars);
  • (99 points): n300n \leq 300, q=0q = 0, x1=1x_1=1 and xi<ix_i<i for 2in2\le i \le n (the network is a tree with the root at node 11);
  • (99 points): n300n \leq 300, q=0q = 0, for all 1i,jn1 \leq i,j \leq n there is such kk that pi,k=jp_{i,k}=j (the network is a cycle);
  • (1313 points): n300n \leq 300, q=0q = 0;
  • (2525 points): n300n \leq 300;
  • (1010 points): n2103n \leq 2\cdot{10}^3, q=0q = 0;
  • (1212 points): without additional restrictions.