#P3243. [HNOI2015] 菜肴制作
[HNOI2015] 菜肴制作
Description
The famous gourmet Xiao A is invited to the ATM Grand Hotel to evaluate dishes. The hotel prepares dishes and numbers them from to in decreasing order of estimated quality; dish has the highest estimated quality.
Due to flavor pairing, some dishes must be prepared before others. Specifically, there are constraints of the form “dish must be prepared before dish ,” which we abbreviate as .
Now, the hotel wants to find an optimal preparation order so that Xiao A can eat higher-quality dishes as early as possible:
That is,
- Subject to all constraints, dish should be prepared as early as possible.
- Subject to all constraints and dish being as early as possible, dish should be as early as possible.
- Subject to all constraints and dishes and being as early as possible, dish should be as early as possible.
- Subject to all constraints and dishes , , and being as early as possible, dish should be as early as possible.
- And so on.
Example 1: There are dishes with two constraints and . Then the preparation order is .
Example 2: There are dishes with two constraints and . Then the preparation order is .
In Example 1, consider dish first. Because of and , dish can only be prepared after dishes and are finished. According to item 3, dish should be prioritized over dish , so the first three dishes are . Next, consider dish , and the final order is .
In Example , preparing dish first does not violate any constraints. Next, when considering dish , there is the constraint , so prepare first and then . Next, when considering dish , there is the constraint , so prepare first and then , resulting in the final order . Now you need to compute this optimal preparation order. If there is no solution, output Impossible!.
Input Format
The first line contains a positive integer , the number of test cases. Then follow test cases. For each test case: The first line contains two space-separated positive integers and , the number of dishes and the number of precedence constraints. The next lines each contain two positive integers , meaning dish must be prepared before dish .
Output Format
Output exactly lines. Each line contains integers representing the optimal preparation order, or Impossible! if there is no solution.
3
5 4
5 4
5 3
4 2
3 2
3 3
1 2
2 3
3 1
5 2
5 2
4 3
1 5 3 4 2
Impossible!
1 5 2 4 3
Hint
-
Sample Explanation:
In the second test case, it simultaneously requires dish before dish , dish before dish , and dish before dish , which is impossible to satisfy, so there is no solution.
-
Constraints:
For of the testdata, , .
There may be completely identical constraints among the constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号