Ordering Tasks
Input: standard input
Output: standard output
Time Limit: 1 second
Memory Limit: 32 MB
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.
Input
The input will consist of several instances of the problem. Each instance begins with a line containing two integers, 1 <= n <= 100 and m. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. An instance with n = m = 0 will finish the input.
Output
For each instance, print a line with n integers representing the tasks in a possible order of execution.
Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3
(The Joint Effort Contest, Problem setter: Rodrigo Malta Schmidt)
题目大意:有n个变量,和m个二元组关系。关系(x,y)表示x<y。现在讲所有变量
从小到大来排序,进行输出。
例如:有4个变量a、b、c、d,若a<b,c<b,d<c,则排序后的可能为a<d<c<b,
也有其他可能d<a<c<d。只要输入其中一个就可。
思路:把n个变量看成是n个点,“x<y”看做是一条边,则得到一个有向图。对图的
节点进行排序,使得每一条有向边(x,y)对应的x都在y前边。即所谓的拓扑排序。
DFS进行拓扑排序,如果存在有向环,则不存在拓扑排序,否则就将访问完的结点
假如到当前拓扑序列的前边。具体过程参考代码。
参考:算法竞赛入门经典(第二版)P168~169