Leaf Village and Democracy
Practice
3 (5 votes)
Hard
Problem
89% Success 204 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

After his rigorous training with Jiraiya, Naruto came back to the Leaf Village and decided to visit the Zoo of summoning animals.

enter image description here

There are N summoning animals numbered from 1 to N in the Zoo, all located currently in one common territory. For some reason, recently the summoning animals started fighting with each other and they are making a massive damage. Since the Hokage of the Leaf Village is a democracy fanatic, she decided that all the visitors of the Zoo should decide how to separate the summoning animals into distinct territories, so hopefully they will stop fighting.

The visitors are sending their requests. The total number of requests is denoted by Q.

Each request is in one of the following forms:

  1. \(SEPARATE(T_1, T_2, \ldots, T_K)\) - a visitor making this request wants to separate summoning animals into K territories, where \(T_i\) is a list of summoning animals forming the \(i^{th}\) territory.

  2. \(CANCEL(T_1, T_2, \ldots, T_K)\) - a visitor making this request wants to cancel a single request of separation the summoning animals into K territories \(T_1, T_2, \ldots, T_K\) described as in the separate request. The cancel request cancels exactly one separation request into this territories, which were requested before and not yet canceled. If there is no such separation request to cancel then nothing happens.

  3. \(ASK()\) - a visitor making this request wants to know how the Zoo will group all the summoning animals into territories based on all not canceled requests made so far. The Zoo is using the following rule to do that:

    Any two summoning animals cannot be assigned to the same territory if there is at least one present request of a visitor who wants them to be into separated territories. Moreover, the Zoo wants to minimize the number of total territories.

The task in this problem is to handle all Q requests made by the visitors.

Constraints:

\(1 \leq N \leq 2000\)
\(1 \leq Q \leq 2000\)
\(1 \leq K \leq N\)

Input format:

In the first line there are two space separated integers N and Q, denoting respectively the number of summoning animals in the Zoo and the number of requests to handle. Description of Q requests follow. Each of them is given in one of the 3 below formats:

  1. If it starts with a line \(\texttt{SEPARATE K}\), it denotes a separate request into K territories. Each such request is followed by exactly K lines. In the \(i^{th}\) of them there is a single integer M followed by a list of space separated integers \(e_{i_1}, e_{i_2}, …, e_{i_M}\) denoting the summoning animals assigned to the \(i^{th}\) territory by this request. In all these K lines, all integers denoting summoning animals are distinct and there are exactly N of them.

  2. If it starts with a line \(\texttt{CANCEL K}\), it denotes a cancel request into K territories. Each such request is followed by exactly K lines. In the \(i^{th}\) of them there is a single integer M followed by a list of space separated integers \(e_{i_1}, e_{i_2}, …, e_{i_M}\) denoting the summoning animals assigned to the \(i^{th}\) territory in this request. In all these K lines, all integers denoting summoning animals are distinct and there are exactly N of them.

  3. If it contains a single word \(ASK\), it denotes an ask request.

Output format:

For each ask request, output the answer to this request in the following format. In the first line print integer K denoting the number of territories that the Zoo decided to use. Then, output exactly K lines denoting separation of the summoning animals into these territories. In the \(i^{th}\) of them an increasing list of M space separated integers \(e_{i_1} < e_{i_2} < … < e_{i_M}\) denoting the numbers of summoning animals assigned to the \(i^{th}\) territory by the Zoo. The \(i^{th}\) of these territories have to contain the summoning animal denoted by the smallest integer which is not yet assigned to any previously printed territory for this request. For more details please refer to the sample.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
12 votes
Tags:
AlgorithmsEasyGreedy AlgorithmsString Manipulationsorting
Points:30
4 votes
Tags:
MathematicsMediumOpenApprovedNumber Theory
Points:30
14 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMediumOpen