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. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  2. Python版本微信跳一跳,软件配置

    一.安装python3的环境: 直接从python官方网站下载python3的安装包,直接安装. 记得将python3放到PATH环境变量中,安装的过程中在该配置地方打钩就可以了. 如果安装的过程中出 ...

  3. 3-22 Ruby 编码规则(个人整理)

    编码规则 https://github.com/thoughtbot/guides/tree/master/style/ruby *Use a trailing comma after each it ...

  4. vue新手入门指导,一篇让你学会vue技术栈,本人初学时候的文档

    今天整理文档突然发现了一份md文档,打开一看 瞬间想起当年学习vue的艰难路,没人指导全靠自己蒙,下面就是md文档内容,需要的小伙伴可以打开个在线的md编译器看一看,我相信不管是新人还是老人  入门总 ...

  5. Leetcode 105

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. SQL 函数 DateDiff()

    DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]]) 描述 返回两个日期之间的时间间隔. 语法 DateDiff( ...

  7. Oracle数据库的“健康指示器”——事件(events)

    Oracle数据库“赢在”在架构,这话还是有道理的,除了锁(lock)这个最大的架构上的“赢点”之外,Oracle事件(event)也是另外一个很重要的架构上的“赢点”,因为当今的Oracle数据库, ...

  8. Cattle学习笔记

    Cattle学习笔记  

  9. Idea热部署jrebel失败

    Idea热部署jrebel

  10. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...