拓扑排序模版题型:

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,要求对所有任务进行排序,使得前面的任务应该先于后面的任务,输出一组解

本题有坑!见代码

 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 #define maxn 4000

 int c[maxn], topo[maxn], t, n, m;
 bool G[maxn][maxn];

 bool dfs(int u)
 {
     c[u] = -;
     ; v < n; v++)
     {
         if (G[u][v])
             )
                 return false;
             else if (!c[v])
                 dfs(v);
     }
     c[u] = ;
     topo[--t] = u;
     return true;
 }

 bool toposort()
 {
     t = n;
     memset(c, , sizeof c);
     ; u < n; u++)
         if (!c[u])
             if (!dfs(u))
                 return false;
     return true;
 }

 int main()
 {
      && n)//因为m的值可能为0!
     //while (scanf("%d%d", &n, &m) && m && n)
     {
         memset(G, , sizeof G);
         int _u, _v;
         while (m--)
             scanf(][_v - ] = ;
         if (toposort())
         {
             ; i < n - ; i++)
                 printf();
             printf(] + );
         }
         else
             printf("No\n");
     }
     system("pause");
     ;
 }

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

  1. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  2. UVa 10305 (拓扑排序) Ordering Tasks

    题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...

  3. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

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

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

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

    题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...

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

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  7. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

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

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

随机推荐

  1. 用 eclipse 创建一个简单的 meaven spring springMvc mybatis 项目

    下面是整体步骤: 1: 先创建一个Maven 项目: 选择跳过骨架: 因为要搭建的是 web 项目  所以这个地方选择 war 包; 点击完成 这样就完成 Maven项目的搭建: 接下俩 先把 Mav ...

  2. PHP设计模式一:工厂方法设计模式

    一.什么是工厂方法模式 作为一种创建型设计模式,工厂方法模式就是要创建“某种东西”.对于工厂方法,要创建的“东西”是一个产品,这个产品与创建它的类之间不存在绑定. 实际上,为了保持这种松耦合,客户会通 ...

  3. LeetCode 665. Non-decreasing Array (不递减数组)

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  4. BZOJ-1225-[HNOI2001] 求正整数

    Description 对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m.例如:n=4,则m=6,因为6有4个不同整数因子1,2,3,6:而且是最小的有4个因子的整数. Input n ...

  5. [板子]segTree

    segTree 参考:http://www.cnblogs.com/TenosDoIt/p/3453089.html#c 初学者建议先参考上面“一步一步理解线段树”学习理论. 在这里Code分别为区间 ...

  6. Spring框架学习之高级依赖关系配置(二)

    紧接着上篇内容,本篇文章将主要介绍XML Schema的简化配置和使用SpEL表达式语言来优化我们的配置文件. 一.基于XML Schema的简化配置方式 从Spring2.0以来,Spring支持使 ...

  7. Linux学习(十七)压缩与打包

    一.关于打包和压缩 打包和压缩的最大意义在于减少文件传输中需要的流量.打包的方式大概有tar命令,zip命令.压缩的方式有gzip,bzip2,xz.tar命令可以通过参数将压缩和打包在一起执行. 二 ...

  8. ELK 完整部署和使用 - 每天5分钟玩转 Docker 容器技术(90)

    上一节已经部署了容器化的 ELK,本节讨论如何将日志导入 ELK 并进行图形化展示. 几乎所有的软件和应用都有自己的日志文件,容器也不例外.前面我们已经知道 Docker 会将容器日志记录到 /var ...

  9. 0_Simple__inlinePTX + 0_Simple__inlinePTX_nvrtc

    在核函数代码中加入并行线程执行(Parallel Thread eXecution,PTX),通过汇编指令获取得有关线程束的信息.并且在静态代码和运行时编译两种条件下使用. ▶ 源代码:静态使用 #i ...

  10. Windows下caffe的配置和调用caffe库(二)

    二. Caffe库的调用: 新建空白项目,将配置管理器更改为x64运行方式.(release和Debug均可). Debug配置: 1)      包含目录: D:\caffe-master\incl ...