#P3243. [HNOI2015] 菜肴制作

[HNOI2015] 菜肴制作

Description

The famous gourmet Xiao A is invited to the ATM Grand Hotel to evaluate dishes. The hotel prepares nn dishes and numbers them from 11 to nn in decreasing order of estimated quality; dish 11 has the highest estimated quality.

Due to flavor pairing, some dishes must be prepared before others. Specifically, there are mm constraints of the form “dish ii must be prepared before dish jj,” which we abbreviate as (i,j)(i, j).

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,

  1. Subject to all constraints, dish 11 should be prepared as early as possible.
  2. Subject to all constraints and dish 11 being as early as possible, dish 22 should be as early as possible.
  3. Subject to all constraints and dishes 11 and 22 being as early as possible, dish 33 should be as early as possible.
  4. Subject to all constraints and dishes 11, 22, and 33 being as early as possible, dish 44 should be as early as possible.
  5. And so on.

Example 1: There are 44 dishes with two constraints (3,1)(3, 1) and (4,1)(4, 1). Then the preparation order is 3,4,1,23, 4, 1, 2.

Example 2: There are 55 dishes with two constraints (5,2)(5, 2) and (4,3)(4, 3). Then the preparation order is 1,5,2,4,31, 5, 2, 4, 3.

In Example 1, consider dish 11 first. Because of (3,1)(3, 1) and (4,1)(4, 1), dish 11 can only be prepared after dishes 33 and 44 are finished. According to item 3, dish 33 should be prioritized over dish 44, so the first three dishes are 3,4,13, 4, 1. Next, consider dish 22, and the final order is 3,4,1,23, 4, 1, 2.

In Example 22, preparing dish 11 first does not violate any constraints. Next, when considering dish 22, there is the constraint (5,2)(5, 2), so prepare 55 first and then 22. Next, when considering dish 33, there is the constraint (4,3)(4, 3), so prepare 44 first and then 33, resulting in the final order 1,5,2,4,31, 5, 2, 4, 3. 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 tt, the number of test cases. Then follow tt test cases. For each test case: The first line contains two space-separated positive integers nn and mm, the number of dishes and the number of precedence constraints. The next mm lines each contain two positive integers x,yx, y, meaning dish xx must be prepared before dish yy.

Output Format

Output exactly tt lines. Each line contains nn 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 11 before dish 22, dish 22 before dish 33, and dish 33 before dish 11, which is impossible to satisfy, so there is no solution.

  • Constraints:

    For 100%100\% of the testdata, n,m105n, m \le 10^5, 1t31 \le t \le 3.

    There may be completely identical constraints among the mm constraints.

Translated by ChatGPT 5