Problem D
Pick-up Sticks
Languages
en
sv
Miss Aspen has been persuaded by a good friend to play Pick-up Sticks. In the game, there are many sticks thrown in a pile. A move consists of trying to pick up a stick without making any other stick move. If you succeed, you get the stick, and to win, you need to have the most sticks at the end of the game.
More formally, there are $N$ different sticks, each depending on groups of other sticks. A group is a set of sticks such that you can remove all the sticks in the group except one without making any of the sticks that depend on the group move. Note that a stick can belong to multiple groups, and not all sticks necessarily belong to any group.
However, it turns out that this is a game that does not suit Miss Aspen very well because she has a very shaky hand. Therefore, she has decided to only play Pick-up Sticks against herself so she doesn’t lose all the time. She realizes, however, that it is harder than it sounds—how do you know which order to remove the sticks in? Or if it’s even possible? Now, Miss Aspen wants your help to find an order in which she can remove the sticks or decide that it is impossible.
Input
The first line contains two integers, $N$ and $G$ ($N \geq 1, G \geq 0$), where $N$ is the number of sticks, and $G$ is the number of unique groups of sticks. Then follows an empty line. After that, $G$ lines describe the groups. On the $i$-th line, group $i$ starts with an integer $g_k$, the number of sticks in the group, followed by $g_k$ integers describing the indices of the sticks in that group.
After that, an empty line.
Then follows $N$ lines describing the sticks. On the $i$-th line, stick $i$ starts with an integer $n_k$, the number of groups that the stick depends on, followed by $n_k$ integers describing the indices of those groups.
It is guaranteed that the input contains at most $3 \cdot 10^5$ numbers in total.
Output
Print a line with $N$ integers, the indices of the sticks in the order they are removed. If there are multiple valid orders, you can print any of them.
If it is not possible to remove all the sticks, print "impossible".
Scoring
Your solution will be tested on several different test groups. To earn points for a group, you must pass all test cases in the group.
Group |
Points |
Constraints |
$1$ |
$49$ |
Every group of sticks contains exactly one stick. |
$2$ |
$7$ |
There are at most $3000$ number in the input. |
$3$ |
$46$ |
No additional constraints. |
Explanation
In the first example case, no stick depends on stick $4$. Therefore, we can remove it first. Among the remaining sticks, no stick depends on stick $3$, so we remove it next. Then, no stick depends on stick $2$, so we can remove it. Finally, no stick depends on stick $1$, so we can remove it as well. This gives us the order $4$ - $3$ - $2$ - $1$.
In the second example case, all sticks depend on each other, and we cannot remove any of them to begin with. Therefore, it is impossible to find a valid order.
In the third example case, we can first remove stick $1$. This is a valid move because, even though stick $2$ depends on group $2$, which includes stick $1$, there is still stick $3$ left, which means stick $2$ won’t move. Afterward, no sticks depend on stick $2$, so we can remove it. Finally, since sticks $3$ and $4$ do not depend on each other, we can remove them too. One possible valid order is $1$ - $2$ - $4$ - $3$.
Sample Input 1 | Sample Output 1 |
---|---|
4 3 1 1 1 2 1 3 0 1 1 2 1 2 3 1 2 3 |
4 3 2 1 |
Sample Input 2 | Sample Output 2 |
---|---|
3 3 1 1 1 2 1 3 1 2 1 3 1 1 |
impossible |
Sample Input 3 | Sample Output 3 |
---|---|
4 3 1 2 2 1 3 2 3 4 1 1 2 2 3 0 0 |
1 2 4 3 |