#P9764. Rope Arrangement with Equal Node Spacing
Rope Arrangement with Equal Node Spacing
Rope Arrangement with Equal Node Spacing
We are given (n) ropes. The (i)-th rope has length (s_i) (in centimeters) and contains (m_i) nodes located at positions (p_{i,1}, p_{i,2}, \dots, p_{i,m_i}) (in centimeters from its left endpoint).
We wish to construct an arrangement (a permutation (q) of ({1,2,\dots,n})) by connecting the ropes from left to right. In the final arrangement the ropes are concatenated without gaps; that is, the right endpoint of rope (q_i) (for (1 \le i < n)) is connected with the left endpoint of rope (q_{i+1}).
After connection, each rope's nodes will be shifted by the total length of all ropes placed before it. Let the overall sequence of nodes (ordered from left to right) be (a_1,a_2,\dots,a_{M}) (where (M = \sum_{i=1}^{n} m_{q_i})). The arrangement is valid if the distances between consecutive nodes are equal, i.e.,
[
a_{j+1} - a_j = d \quad \text{for all } 1 \le j < M.
]
Notice that for any rope which contains at least two nodes (i.e. (m_i \ge 2)), the nodes on that rope must themselves be in an arithmetic progression. Therefore, for such a rope (i) we must have
[
p_{i,2} - p_{i,1} = p_{i,3} - p_{i,2} = \cdots = d.
]
Furthermore, when connecting two ropes in the order (q_i) then (q_{i+1}), if we denote by (f_i = p_{q_i,1}) and (l_i = p_{q_i,m_{q_i}}) the first and last nodes of rope (q_i) (if any), then the connection imposes the condition
[
d = s_{q_i} + f_{q_{i+1}} - l_{q_i} \quad \text{for } 1 \le i < n.
]
Your task is to output one valid permutation (q) of ({1,2,\dots,n}) (as a sequence of rope indices separated by spaces) that satisfies all these conditions. If no such permutation exists, output exactly the string No
.
Note: It is guaranteed that if a rope has (m_i \ge 2) then all consecutive differences (p_{i,j+1}-p_{i,j}) are equal. However, ropes having 0 or 1 node do not impose an internal common difference constraint.
inputFormat
The input begins with a single integer (n) indicating the number of ropes. Each of the following (n) lines describes a rope. Each rope description starts with two integers (s_i) and (m_i) (the length and the number of nodes), followed by (m_i) integers (p_{i,1}, p_{i,2}, \dots, p_{i,m_i}) representing the positions (in centimeters) of the nodes along the rope.
outputFormat
If a valid permutation exists, print the permutation as (n) space-separated integers. Otherwise, print the string No
.
sample
3
10 2 2 6
12 2 0 4
7 2 -4 0
1 2 3