ACM 之 M - Ordering Tasks

Description

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

理解

拓扑排序的一个比较典型的题目.题目大意是:每一项任务都有一个编号,输入的两个编号表示只有做过前边的编号任务才能接着去做后边的编号任务,让我们输出做任务的顺序.

代码部分

#include
#include
using namespace std;
int map[101][101],inde[101];
void topu(int a[101][101],int d[101],int n)
{
    int f=0;
    for(int i=0;i>m>>n)
    {
        if(m==0&&n==0)
            return 0;
        int a,b;
        memset(map,0,sizeof(map));
        memset(inde,0,sizeof(inde));
        for(int i=1;i<=n;i++)
        {
            cin>>a>>b;
            if(!map[a][b])
            {
                map[a][b]=1;
                inde[b]++;
            }
        }
        topu(map,inde,m);
        cout<

意见反馈 || 任何建议

联系我(新浪)
邮箱:[email protected]

你可能感兴趣的:(ACM 之 M - Ordering Tasks)