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

拓扑排序:

代码1:

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
///拓扑排序学习题
int main()
{
int n,m,a,b,ans[];
while(scanf("%d%d",&n,&m))
{
if(!(n+m)^)break;
int mp[][]={},vis[]={},mark[]={};///mark用来记录是否有比自己优先的 如果有就
for(int i=;i<m;i++) /// 加1 可能有多个比自己优先的,如果没有比自己优先的 直接输出
{ ///mp记录是否有排在自己后面的 如果有就把mark减一 表示我已经输出了
scanf("%d%d",&a,&b); /// 对你没限制了 至于其他人对你有没有限制我不管了
mark[b]++; ///vis看是否被记录 ,记录了变为1
mp[a][b]=;
}
int j=,temp;
while(j<n)
{
temp=-;
for(int i=;i<=n;i++)
if(!vis[i]&&!mark[i])
{
ans[j++]=i;
temp=i;
break;
}
if(temp<)break;///不存在没记录的元素了 就停止
vis[temp]=; ///mark一下
for(int i=;i<=n;i++)
if(mp[temp][i]==)mark[i]--;
}
for(int i=;i<n;i++)
printf("%d ",ans[i]);
putchar('\n');
}
}

代码2:

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
///拓扑排序学习题 递归形式dfs 选中一个未排序的元素进行dfs把在他之后的装进ans(倒着装)并标记。 int n,m,a,b,ans[],mp[][]={},vis[]={},k;
bool dfs(int last)
{
vis[last]=-;
for(int i=;i<=n;i++)
if(mp[last][i])
{
if(vis[i]==)dfs(i);
else if(vis[i]==-)return false;//形成了回路 无法继续排序 -1表示i比last先入栈。
}
ans[--k]=last;
vis[last]=;
return true;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(!(n+m))break;
k=n;
memset(vis,,sizeof(vis));
memset(mp,,sizeof(mp));
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
mp[a][b]=;
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
if(!dfs(i))break;
}
}
for(int i=k;i<n;i++)
printf("%d ",ans[i]);
putchar('\n');
}
}

Ordering Tasks 拓扑排序的更多相关文章

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

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

  2. M - Ordering Tasks(拓扑排序)

    M - Ordering Tasks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descri ...

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

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

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

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

  5. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  6. UVA10305 Ordering Tasks (拓扑序列)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...

  7. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  8. 拓扑排序(Topological Order)UVa10305 Ordering Tasks

    2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...

  9. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

随机推荐

  1. CSS sprites(css 精灵):将小图标整合到一张图片上

    一.什么是css sprites css sprites直译过来就是CSS精灵.通常被解释为“CSS图像拼合”或“CSS贴图定位”.其实就是通过将多个图片融合到一张图里面,然后通过CSS backgr ...

  2. 16 Managing Undo

    16 Managing Undo 从Oracle11g开始,在默认安装中oracle会自动管理undo, 典型安装中不需要DBA介入配置,然而,如果选择了flash back特性,你就需要进行一些un ...

  3. Java 的对象和类

    Java 是一种面向对象的语言.作为一个面向的语言,Java 具有面向对象的特性,Java 能够支持下面的一些基本概念 − 多态(Polymorphism) 继承(Inheritance) 封装(En ...

  4. python-day21--序列化模块模块

    什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化   序列化的目的: 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性. ...

  5. cf188C(最大子段和&&思维)

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. 使用opatch工具 打补丁Patch 21352635 -(Database Patch Set Update 11.2.0.4.8)

    Patch 21352635 - Database Patch Set Update 11.2.0.4.8 一.OPatch工具检查及升级 OPatch工具包,在安装目录$ORACLE_HOME下,P ...

  7. vue打包后图片找不到情况

    打包之前需要修改如下配置文件: 配置文件一:build>>>utils.js (修改publicPath:"../../" , 这样写是处理打包后找不到静态文件( ...

  8. @Resource与@Autowired注解的区别

    一.写本博文的原因 年初刚加入到现在的项目时,在使用注解时我用的@Resource.后来,同事:你怎么使用@Resource注解?我:使用它有错吗?同事:没错,但是现在都使用@Autowired.我: ...

  9. python读写csv时中文乱码问题解决办法

    https://www.cnblogs.com/shengulong/p/7097869.html 参考1 参考2 参考3 CSV是英文Comma Separate Values(逗号分隔值)的缩写, ...

  10. display: table 实现menu等高居中排列

    display: table 属性,顾名思义,就是就像表格一样陈列元素,设置这个属性之后,就具有了表格所特有的某些特性,比如居中对齐之类的. 本篇文章要实现的需求也是非常常见的——左侧栏menu菜单居 ...