P14999 [Nordic OI 2019] Thieves and Prisons

Description

There are $n$ thieves and $k$ prisons. A thief is either on the run or caught in a prison. Initially all thieves are on the run. A thief who is on the run can be caught by the police, and then ends up in one of the prisons. A thief who is on the run can also open the gate of a prison. Then every thief in that prison is released from the prison. It would be pointless to open the gate of an empty prison, so that never happens. You are given a list of $m$ events of the form "thief $x$ has been caught" or "thief $x$ has opened the gate of a prison". Your task is to find a prison assignment that corresponds to the events, or determine that it is not possible.

Input Format

The first input line has three integers $n$, $k$ and $m$: the number of thieves, prisons and events. The thieves and prisons are numbered $1, 2, \ldots, n$ and $1, 2, \ldots, k$. After this, there are $m$ lines that describe the events. Each event is "C $x$" (thief $x$ has been caught) or "O $x$" (thief $x$ opens the gate of a prison).

Output Format

Print a valid prison assignment that consists of $m$ integers: for every event the corresponding prison. If there are no solutions, print "IMPOSSIBLE".

Explanation/Hint

**Subtask 1 (8 points)** - $1 \leq n, m \leq 10$ - $k = 2$ **Subtask 2 (13 points)** - $1 \leq n, k, m \leq 10^5$ - $n = k$ **Subtask 3 (14 points)** - $1 \leq n, m \leq 10^5$ - $k = 2$ **Subtask 4 (18 points)** - $1 \leq n, k, m \leq 500$ **Subtask 5 (47 points)** - $1 \leq n, k, m \leq 10^5$