[拓扑排序]Ordering Tasks UVA - 10305

拓扑排序模版题型:

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组任务关系(u,v),即先有u,才能有v,要求对所有任务进行排序,使得前面的任务应该先于后面的任务,输出一组解

本题有坑!见代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 4000
 5
 6 int c[maxn], topo[maxn], t, n, m;
 7 bool G[maxn][maxn];
 8
 9 bool dfs(int u)
10 {
11     c[u] = -1;
12     for (int v = 0; v < n; v++)
13     {
14         if (G[u][v])
15             if (c[v] < 0)
16                 return false;
17             else if (!c[v])
18                 dfs(v);
19     }
20     c[u] = 1;
21     topo[--t] = u;
22     return true;
23 }
24
25 bool toposort()
26 {
27     t = n;
28     memset(c, 0, sizeof c);
29     for (int u = 0; u < n; u++)
30         if (!c[u])
31             if (!dfs(u))
32                 return false;
33     return true;
34 }
35
36 int main()
37 {
38     while (scanf("%d%d", &n, &m) ==2 && n)//因为m的值可能为0!
39     //while (scanf("%d%d", &n, &m) && m && n)
40     {
41         memset(G, 0, sizeof G);
42         int _u, _v;
43         while (m--)
44             scanf("%d%d", &_u, &_v), G[_u - 1][_v - 1] = 1;
45         if (toposort())
46         {
47             for (int i = 0; i < n - 1; i++)
48                 printf("%d ", topo[i] + 1);
49             printf("%d\n", topo[n - 1] + 1);
50         }
51         else
52             printf("No\n");
53     }
54     system("pause");
55     return 0;
56 }
时间: 2024-10-17 19:54:33

[拓扑排序]Ordering Tasks UVA - 10305的相关文章

UVa 10305 (拓扑排序) Ordering Tasks

题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向边,如果得到的图是有向无环图DAG(Directed Acyclic Graph),则是存在拓扑排序的,如果存在有向环,则不存在拓扑排序. 注意输入数据里面m可能等于0的情况,就因为这个WA了两次. 1 //#define LOCAL 2 #include <iostream> 3 #includ

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

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 拓扑排序题解

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

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. Each instance begins with

uva 10305 Ordering Tasks(拓扑排序)

拓扑排序,不用判断是否有环,dfs挺简单的 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int map[105][105]; int visit[105]; int c[105]; int n,m,t; void dfs(int x) { visit[x] = 1; for(int i=1; i<=n; i++) { if(!visit[i]&&map[i][x]==1)

UVA 10305 Ordering Tasks (拓扑排序)

题意:给你n个点.m个关系,每个关系两个点u.v,表示u小于v,叫你输出任意一个序列保证满足所有给定的关系 例如:n=3 m=2 1 2 3 1 3 2 3 1 2 题解:拓扑排序排的是一个有向无环图(DAG),首先没有回路,否则会失败,其次如果存在G(u,v),则在该序列中u在v前面 实现方法就是遍历每个点,当此点没被标记过就进入递归 然后将此点看做树的根节点(DAG其实可以看做森林),遍历它可以到的所有点,最后从后向前将点加入排序的序列并标记 #include<cstdio> #inclu

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

UVA - 10305 Ordering Tasks(拓扑排序)

题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #i