题目链接:https://cn.vjudge.net/problem/UVA-10305(忍不住uva连接满)

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 mn 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个点(从1开始),还有要求一些点i必须在j前面。输出任意一种排序。

题解:裸的拓扑排序,直接套模板。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define ms(a, b)  memset((a), (b), sizeof(a))
16 //#define LOCAL
17 typedef long long LL;
18 const int inf = 0x3f3f3f3f;
19 const int maxn = 100+10;
20 const int mod = 1e9+7;
21 int gap[maxn][maxn];
22 int topo[maxn], c[maxn], t;
23 int n, m;
24 bool dfs(int u){
25     c[u] = -1;
26     for(int v = 1; v<=n; v++)   if(gap[u][v]){
27         if(c[v]< 0) return false;
28         else if(!c[v] && !dfs(v) )  return false;
29     }
30     c[u] = 1;
31     topo[--t] = u;
32     return true;
33 }
34 bool toposort(){
35     t = n+1;
36     ms(c, 0);
37     for(int u = 1; u<=n ;u++)   if(!c[u])
38         if(!dfs(u)) return false;
39     return true;
40 }
41 int main()
42 {
43     #ifdef LOCAL
44         freopen("input.txt" , "r", stdin);
45     #endif // LOCAL
46     while(~scanf("%d%d", &n, &m)){
47         if(n==0 && m==0)    break;
48         ms(gap, 0);
49         for(int i = 0;i<m;i++){
50             int a, b;
51             scanf("%d%d", &a, &b);
52             gap[a][b] = 1;
53         }
54         if(toposort()){
55             for(int i =1 ; i<=n ;i++)
56                 if(i==1)    printf("%d", topo[i]);
57                 else    printf(" %d", topo[i]);
58             printf("\n");
59         }
60     }
61     return 0;
62 }

View Code


转载于:https://www.cnblogs.com/denghaiquan/p/6668417.html

UVA10305 Ordering Tasks相关推荐

  1. Uva 10305 - Ordering Tasks

    Problem F: Ordering Tasks Input: standard input; Output: standard output Time Limit: 1 second;  Memo ...

  2. 【刘汝佳可运行代码】Ordering Tasks UVA - 10305【两种解法】

    立志用最少的代码做最高效的表达 John has n tasks to do. Unfortunately, the tasks are not independent and the executi ...

  3. Ordering Tasks UVA - 10305(拓扑排序)

    在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...

  4. UVA 10305 Ordering Tasks (拓扑排序)

    题意:给你n个点.m个关系,每个关系两个点u.v,表示u小于v,叫你输出任意一个序列保证满足所有给定的关系 例如:n=3 m=2 1 2 3 1 3 2 3 1 2 题解:拓扑排序排的是一个有向无环图 ...

  5. Ordering Tasks

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. python 拓扑排序_拓扑排序(topsort)算法详解

    在图论中,由某个集合上的偏序得到全序的策略就是拓补排序算法.拓扑排序常出现在涉及偏序关系的问题中,例如时序的先后.事物的依赖等.针对这些问题拓扑排序通常能有效地给出可行解. 为了便于理解,我们先来看一 ...

  7. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  8. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

  9. ICPC程序设计题解书籍系列之五:吴永辉:《数据结构编程实验》(第2版)

    第1章 简单计算 UVALive2362 POJ1004 HDU1064 ZOJ1048 Financial Management[数学+水题] - 海岛Blog - CSDN博客 POJ1552 H ...

最新文章

  1. 牛客小白月赛5-F题: 圆(circle)
  2. 浪漫的形式有100种,单身的就1种!
  3. 面向自然语言处理的深度学习
  4. 接口测试用例设计思路_用了Swagger2后,接口设计文档,测试用例都不用自己写了,爽...
  5. Java经典编程题50道之十七
  6. 【错误记录】VMware 虚拟机报错 ( 向 VMWare 虚拟机中的 Ubuntu 系统拷贝文件时磁盘空间不足 )
  7. springboot:SpringBoot项目启动成功,但无法访问且提示404
  8. [SAP-SD]Sales Order 中的User Exit开发
  9. 飞桨抠图直播2020.4.1
  10. MVC POST请求后执行javascript代码
  11. awk按ip统计日志数
  12. html文本分类输出,构建中文网页分类器对网页进行文本分类
  13. 计算机用户的注册表,计算机上的注册表在哪里
  14. 斐波那契查找(Fibonacci Search)和折半查找
  15. 《Sibelius 脚本程序设计》连载(三十九) - 4.9 SystemStaff
  16. 为未来元素添加点击事件的两种写法
  17. js面向对象思想封装拖拽功能,兼容pc和移动端
  18. 思科路由器 DHCP配置
  19. SharpMap在web上的应用
  20. 第一次做socket的一些心得

热门文章

  1. 学习笔记Hadoop(十五)—— MapReduce编程进阶
  2. 学习笔记(十九)——Python与数据库交互(mysql、redis)
  3. QT5界面操作2:如何用状态栏显示鼠标坐标
  4. python支付系统开发,python支付整合开发包
  5. linux shell 输出到数据库,linux shell 入门
  6. java数据类型_JAVA的数据类型
  7. 计算机的优势和劣势_100亿倍,中国量子计算机完胜美国,向中方科学家致敬
  8. 怎样在线把别人web前端代码抓下_自学web前端8个月,我是怎样拿下7K薪资的?
  9. python几岁开始学_python编程少儿几岁可以学?有哪些优势?
  10. mysql cpu占用率过高,MySQL高CPU使用率