POJ 3687-Labeling bils (reverse topological sorting)
Labeling bils
| Time Limit:1000 MS |
|
Memory Limit:65536 K |
| Total Submissions:11256 |
|
Accepted:3230 |
Description
Windy hasNBils of distinct weights from 1 unitNUnits. Now he tries to label them with 1NIn such a way that:
No two bils share the same label. The labeling satisfies several constrains like "The ball labeled
AIs lighter than the one labeled
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) andM(0 ≤M≤ 40,000). The nextMLine each contain two integersAAndBIndicating the ball labeledAMust be lighter than the one labeledB. (1 ≤A, B≤N) There is a blank line before each test case.
Output
For each test case output on a single line the Ball' weights from label 1 to labelN. If several solutions exist, you shoshould 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
54 04 11 14 21 22 14 12 14 13 2
Sample Output
1 2 3 4-1-12 1 3 41 3 2 4
There are T groups of test data. The first row of each group of test data has two numbers n and m. It indicates that there are n balls. In the next m rows, each row has two numbers a and B, indicating that the ball is lighter than the B ball, so that you can output the weight of the ball from small to large. If it is the same weight, output in Lexicographic Order.
Thought: This is slightly different from the previous topological sorting, not considering the inbound degree of 0. Because assume n is 4, 4 <1. then, the output columns are, instead of 4, 1, 2, and 3. therefore, we should first put the heaviest one behind it, and then output it in the lexicographically ascending order.
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; int map [210] [210]; int out [210]; void topo (int n) {int I; int cnt = n; int weight [210]; priority_queue
Q; // priority queue, sorted in ascending order. For (I = 1; I <= n; I ++) {if (out [I] = 0) q. push (I) ;}while (! Q. empty () {int k = q. top (); q. pop (); weight [k] = cnt --; for (I = 1; I <= n; I ++) {if (map [I] [k]) {out [I] --; if (out [I] = 0) q. push (I) ;}}if (cnt> 0) printf ("-1 \ n"); else {for (I = 1; I