Given a forest of split nodes, order the leaf labels so each split node's leaves are consecutive, choosing the lexicographically smallest sequence, then answer position queries.
Medium7TreeDFSSortingGreedyNo attempts yetTime limit8sMemory limit512 MBEach development department at Ishimatsu Electronics owns one product family, such as disks and storage, network equipment, or mobile phones. A department covers so many kinds of product that the staff in the product management division had trouble deciding which category a new product belongs to. One of them proposed a drawing called a category diagram.
A category diagram is drawn on one large sheet of paper. The names of the development departments go on the upper side of the sheet. These are the start nodes. Questions that tell product features apart go in the middle. These are the split nodes. The category names go on the lower side. These are the end nodes. Each start node is joined to a single split node or a single end node, and every line going down from a split node is labeled with an answer to that question. To classify a product you begin at the start node of the department that built it, follow the lines downward, and read the name written on the end node you reach.
The drawing is easy to read, but the hand drawn diagrams were messy because lines crossed many times. The staff asked for a clean diagram instead, one in which no two lines cross.
Ignore the text of the questions and use integer labels from 1 to N in place of the category names. The y-coordinate and the label list of each split node fix every connection in the diagram. Now place the end nodes one per position, from position 1 on the left through position N on the right. A placement has no crossing lines exactly when, for every split node, the end nodes hanging below it occupy consecutive positions. Several placements can satisfy this, so take the one whose label sequence, read from position 1 through position N, comes first in lexicographic order.
A query gives one position number. Answer with the label of the end node placed there.
The input consists of several datasets. Each dataset has the following format.
N M Q
split node info 1
split node info 2
...
split node info M
query 1
query 2
...
query Q
The first line holds the number of end nodes N, the number of split nodes M, and the number of queries Q. The next M lines describe one split node each, in the following format.
Y L label1 label2 ...
Y is the y-coordinate of that split node, and a smaller value is higher up. L is the length of the label list, followed by L end node labels. The labels within one list are distinct.
The label lists fix the connections. For an end node label e, let C(e) be the split nodes whose list contains e, ordered by increasing y-coordinate.
A split node is connected to at most one split node above it, so the whole diagram is a forest whose leaves are the end nodes.
The split node lines are followed by Q lines holding one query each. A query is a horizontal position of an end node, and the leftmost position is numbered 1.
The constraints are as follows.
The dataset with N=M=Q=0 marks the end of the input and is not processed.
For each dataset print Q lines. Line i holds the label of the end node placed at the position given by query i. Use the placement that has no crossing lines and whose label sequence comes first in lexicographic order.
Print one blank line after the output of each dataset.