#P2776. Team Queue
Team Queue
Team Queue
You are given (m) groups and (n) elements, where each element belongs to exactly one group. You need to implement a queue which supports the following operations:
push x: Enqueue the element (x). If the queue already contains one or more elements from the same group as (x), then (x) is inserted immediately after the last element of that group; otherwise, (x) is added to the end of the entire queue.
pop: Dequeue the element at the front of the queue and output it. This operation behaves as in a regular FIFO queue.
inputFormat
The first line contains two integers (m) and (k), where (m) is the number of groups and (k) is the number of operations.\n\nThen follow (m) lines. Each of these lines starts with an integer (p) representing the number of elements in that group, followed by (p) integers which are the elements belonging to that group.\n\nThen follow (k) lines, each containing a command. Each command is either in the format push x
or pop
.
outputFormat
For each pop
command, output the dequeued element on a new line.
sample
2 6
3 1 2 3
2 4
push 1
push 4
push 2
pop
pop
pop
1
2
4
</p>