#HK6977. 「ICPC World Finals 2024」幼儿园
「ICPC World Finals 2024」幼儿园
Description
Taking a group of kindergarten kids to the planetarium isn't easy. You really wanted to do this, to allow every kid a chance to get into the room with the giant telescope and take a look at Jupiter. And now that you're going, you remember the stories from other caretakers that kids can misbehave and leave some nasty surprises in the telescope room for the kids after them. You really want to avoid that.
You know the kids in your group very well. Each kid is jealous of one other kid, who is cooler than them, and they might misbehave in the telescope room if they know the kid they're jealous of will be there at some point after them-not necessarily immediately after them, just at some later point. You thought this would be easy-the coolest kid in the class doesn't have anyone to be jealous of, so she can go first, and then all the other kids in order of coolness. However, you just learned that the coolest kid is an exception-instead of being jealous of someone cooler than herself, she's jealous of some other random kid in the group. This sounds like a disaster!
Fortunately, you also know that each kid has some other kid they really, really like. So whenever a kid in the telescope room is thinking about setting up a surprise, if they know the kid they like is going to be in the room after them and before the kid they're jealous of, they will refrain from misbehaving. To make this formal, if a kid is jealous of kid , and really likes kid , then there's a risk will misbehave and set up a surprise in the telescope room if will be in the telescope room after , and will be there either before or after .
Can you figure out an order in which the kids can go to the telescope room so no surprises occur?
Input
The first line contains an integer (), the number of kids in your group. The kids are indexed from to in decreasing order of coolness. Each of the next lines describes one of the kids. The of these lines contains two integers: , the index of the kid that the kid is jealous of, and , the index of the kid that the kid really, really likes ($1 \leq j_{i}, l_{i} \leq n, j_{i} \neq l_{i}, j_{i} \neq i, l_{i} \neq i$, and for all except ).
Output
Output a line containing integers, the order in which the kids should enter the telescope room. If there are multiple ways to order the children, output any of them. If no order exists output impossible.
4
4 2
1 4
2 4
2 1
1 2 3 4
4
2 3
1 4
2 1
1 2
impossible