poj3687

maksyuki 发表于 oj 分类,标签:
0

Labeling Balls

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

1. No two balls share the same label.

2. The labeling satisfies several constrains like "The ball labeled witha is lighter than the one labeled with b".

Can you help windy to find a solution?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The next M line each contain two integers aand b indicating the ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, b ≤ N) There is a blank line before each test case.

Output

For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.

Sample Input

5

4 0

4 1
1 1

4 2
1 2
2 1

4 1
2 1

4 1
3 2

Sample Output

1 2 3 4

-1

-1

2 1 3 4

1 3 2 4

Source

POJ Founder Monthly Contest – 2008.08.31, windy7926778

 

题目类型:拓扑排序

算法分析:这里若将重量小的球向重量大的球连边,则不能保证正确性。所以反过来想,将重量大的球向重量小的球连边,每次给标号尽可能大的,入度为0的点递减赋值,这样就能保证标号小的球重量尽可能小。注意图中可能出现平行边,可以处理也可以不处理