Given the surviving shuffled entries of a table of next occurrences for an unknown alphabet size, recover the lexicographically smallest original sequence or report that none exists.
Hard9GreedyGraphImplementationSortingNo attempts yetTime limit2sMemory limit512 MBProfessor Tupids dug up a new archaeological document during a recent survey.
The document is n symbols long and uses an alphabet of c symbols. To study its meaning the professor gave each symbol a natural number from 1 to c, so the document can be written as a sequence of n natural numbers between 1 and c.
To examine the relations between the symbols the professor built a large table with n rows and c columns. The value written in row i, column j is the position of the first occurrence of symbol j after position i in the sequence. When no such position exists, the cell holds 0.
For example, take n=6, c=3 and the sequence [1,3,2,2,1,3]. A few cells of the table are:
One day a fire broke out in the laboratory and the document burned away. The table was not lost completely, but it was damaged. The record of the value of c is gone, some cells are no longer legible, and an assistant stored the table so badly that the numbers inside each row were shuffled.
The professor wants to recover the original document from what is left. Infinitely many sequences can fit, so the professor asks for the lexicographically smallest one. If the content of the table is wrong, no sequence fits at all, and then it is enough to bring the professor that sad news.
Read the damaged table and recover the original sequence.
The first line holds the length n (1≤n≤300000) of the original sequence.
Line i of the next n lines holds an integer ci (0≤ci≤n−i) followed by ci natural numbers. These ci numbers are the numbers still legible in row i of the table. They are at most n and pairwise different, and the order they are written in can differ from the order in the original table.
The sum of all ci is at most 300000.
If a sequence satisfies the table, print its n numbers on one line, separated by single spaces. If several sequences satisfy the table, print the lexicographically smallest one.
If no sequence satisfies the table, print "No Solution".