M - Ordering Tasks

M - Ordering Tasks

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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

//这题的意思是这样的,第一行输入n,m,两个整数,说明有 1-n 个数,m个要求,接下来m行每行一个要求,a b,a必须放在b前面,输出一种可行的方案mn都为0结束输入。

这个题目其实就是拓扑排序,思路是将有要求的用一个二维数组存起来,和图一样,读完数后,从1开始到n,从有关联的并且未放置过的数一直dfs遍历,找到最后一个,也就是找到没有要放在这个数后面的了,将它们这一串放在没放过的数组后面。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5
 6 int n,m;
 7 bool G[105][105];
 8 int topo[105];
 9 int vis[105];
10 int t;
11
12 bool dfs(int u)
13 {
14     vis[u]=-1;                   //标记为正在遍历的
15     for (int v=1;v<=n;v++)
16     {
17         if (G[u][v])
18         {
19             if (vis[v]==-1) return 0;       //说明组成了环,不能拓扑排序
20             else if (!vis[v]&&!dfs(v)) return 0; //继续遍历未遍历的
21         }
22     }
23     vis[u]=1;       //标记遍历过了
24     topo[t--]=u;    //输出的就是这个数组
25     return 1;
26 }
27
28 bool toposort()
29 {
30     t=n;
31     int i;
32     for (i=1;i<=n;i++)               //将所有数都遍历
33         if (!vis[i]&&!dfs(i)) return 0;
34     return 1;
35 }
36 int main()
37 {
38     int i;
39     int a,b;
40     while (scanf("%d%d",&n,&m)&&n+m)
41     {
42         memset(G,0,sizeof (G));
43         for (i=0;i<m;i++)
44         {
45             scanf("%d%d",&a,&b);
46             G [a][b]=1;          //记录关系
47         }
48         memset(vis,0,sizeof(vis));
49         if (toposort())
50         {
51             for (i=1;i<n;i++)
52             printf("%d ",topo[i]);
53             printf("%d\n",topo[n]);
54         }
55     }
56     return 0;
57 }


 



时间: 2024-10-10 04:16:48

M - Ordering Tasks的相关文章

[SOJ] Ordering Tasks

1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB 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

UVA - 10305 - Ordering Tasks (拓扑排序!)

UVA - 10305 Ordering Tasks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n task

UVA10305 Ordering Tasks【DFS】【拓扑排序】

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

拓扑排序(Topological Order)UVa10305 Ordering Tasks

2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意一对顶点u,v满足如下条件: 若边(u,v)∈E(G),则在最终的线性序列中出现在v的前面 好了,说人话:拓扑排序的应用常常和AOV网相联系,在一个大型的工程中,某些项目不是独立于其他项目的,这意味着这种非独立的项目的完成必须依赖与其它项目的完成而完成,不妨记为u,v,则若边(u,v)∈E(G),代

UVa 10305 - Ordering Tasks【拓扑排序】

Ordering Tasks 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 insta

Ordering Tasks From:UVA, 10305(拓扑排序)

Ordering Tasks From:UVA, 10305 Submit Time Limit: 3000 MS      Special Judge 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 inpu

uva 10305 Ordering Tasks (简单拓扑)

uva 10305 Ordering Tasks 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.

UVa 10305 - Ordering Tasks 拓扑排序题解

Topological Sort题解.本题是简单的入门题目. Topological Sort的思想很简单,就是按没有入度的点,先输出,然后删除这个点的出度.然后输出下一组没有入度的点. 如何实现也是很简单的: 这里使用邻接表,建图的时候反过来建图,建立一个入度邻接表. 然后使用一个vis数组,记录访问过的节点,也可以根据这个信息知道哪些是已经输出的点,这个时候这些点的入度可以不算为当前入度了. #include <stdio.h> #include <vector> using

Ordering Tasks 拓扑排序

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task isonly possible if other tasks have already been executed.InputThe input will consist of several instances of the problem. Each instance begins with a