eolymp
bolt
Try our new interface for solving problems
Problems

Partition of the set

Partition of the set

Consider a set consisting of the first \textbf{n} natural numbers: \textbf{N_n} = \{\textbf{1}, \textbf{2}, ..., \textbf{n}\}. Partition - a representation of this set as the union of one or more non-empty sets. Examples of partitions for \textbf{n} = \textbf{5} are: \{1, 2, 3, 4, 5\} = \{1, 2, 3\} U \{4, 5\} \{1, 2, 3, 4, 5\} = \{1, 3, 5\} U \{2, 4\} \{1, 2, 3, 4, 5\} = \{1, 2, 3, 4, 5\} \{1, 2, 3, 4, 5\} = \{1\} U \{2\} U \{3\} U \{4\} U \{5\} In total there are \textbf{52} partitions of \textbf{N_5}. Note that the partitions that differ only by order of the merged sets did not differ. Partition of the set \textbf{N_n} can be ordered \textit{lexicographically}. \includegraphics{https://static.e-olymp.com/content/cc/cc8a15c4c87b4c7e92efe076a394dd47b084fd99.jpg} \includegraphics{https://static.e-olymp.com/content/cc/cc8a15c4c87b4c7e92efe076a394dd47b084fd99.jpg} In order to determine the order, first define the lexicographic order on subsets of \textbf{N_n}. We say that a set \textbf{A} \textbf{N_n} lexicographically less than the set \textbf{B} \textbf{N_n} and write \textbf{A} < \textbf{B}, if one of the following statements: \begin{itemize} \item There exists \textbf{i}, such that \textbf{i} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{A}, \textbf{i} \includegraphics{https://static.e-olymp.com/content/97/970fb3450de3b7b3d852f20e523df2d748f3f66c.jpg} \textbf{B}, for all \textbf{j} < \textbf{i}: \textbf{j} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{A} iff \textbf{j} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{B}, and there is a \textbf{k} > \textbf{i}, such that \textbf{k} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{B}; \item \textbf{A} \includegraphics{https://static.e-olymp.com/content/cc/cc8a15c4c87b4c7e92efe076a394dd47b084fd99.jpg} \textbf{B} and \textbf{i} < \textbf{j} for all \textbf{i} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{A} and \textbf{j} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{B} \ \textbf{A}. \end{itemize} It is obvious that this relation is a total order on subsets of \textbf{N_n}. Now we define the canonical representation of the partition as a representation in that combines multiple lexicographically. Partitions are ordered lexicographically as follows. Partition of \textbf{N_n} = \textbf{A_1} U \textbf{A_2} U \textbf{...} U \textbf{A_k }lexicographically less than the partition of \textbf{N_n} = \textbf{B_1} U \textbf{B_2} U \textbf{...} U \textbf{B_l}, if there is an \textbf{i}, as \textbf{A_1} = \textbf{B_1}, \textbf{A_2} = \textbf{B_2}, ..., \textbf{A_\{i-1\}} = \textbf{B_\{i-1\}} and \textbf{A_i} < \textbf{B_i}. According to the partition of \textbf{N_n} find the following in the lexicographic order decomposition. \InputFile The input file contains several descriptions of the tests. Each description is the canonical representation of the partition. The first line contains the description of \textbf{n} and \textbf{k} - number of elements in the partitions the set and the number of parts in a partition (\textbf{1} ≤ \textbf{n} ≤ \textbf{200}). Subsequent \textbf{k} rows contain elements of the partition. Elements of each set in ascending order. Describe the tests are separated by blank lines. The last line of input contains two zero. This test should not be handled. Sum of \textbf{n} over all the descriptions do not exceed \textbf{2000}. \OutputFile For each test, the following output in lexicographic order decomposition. If the partition in the input file is the latest in leksigoraficheskom order, display the first in lexicographic order. Use the same format as the input file. Separate partitions from each other by blank lines.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5 2
1 2 3
4 5

5 2
1 3 5
2 4

5 1
1 2 3 4 5

5 5
1
2
3
4
5

0 0
Output example #1
5 2
1 2 3 4
5

5 4
1 4
2
3
5

5 2
1 2 3 5
4

5 4
1
2
3
4 5