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 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 拓扑排序的更多相关文章
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- M - Ordering Tasks(拓扑排序)
M - Ordering Tasks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- UVA10305 Ordering Tasks (拓扑序列)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
随机推荐
- Python BeautifulSoup的使用
2017-07-24 22:39:14 Python3 中的beautifulsoup引入的包是bs4 import requests from bs4 import * r = requests.g ...
- C#快速生成数据数组
需求:生成一个数组,数组里面的值为1-100实现方式:拿到这个需求很多朋友可能会想到一个快速实现的方式如下: ]; ;i<=;i++){ arr[i]=i; } 但是C#提供了一个快速生成的方式 ...
- HTTP URL 字符转义 字符编码 、 RFC 3986编码规范
一.为什么要编码转义 通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如Url参 ...
- [Java代码] Java是自学好还是参加培训班好?
ava 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. 本教程给大家简单介 ...
- CAS操作原理分析
一.CAS简单介绍 CAS:Compare and Swap, 翻译成比较并交换. java.util.concurrent包中借助CAS实现了区别于synchronouse同步锁 ...
- km板子(二分图最大权匹配)
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack- ...
- python-day14--带参数的装饰器+多个装饰器装饰同一个函数
1.# 带参数的装饰器def f1(flag): def f2(func): def inner(*args,**kwargs): if flag: '''执行函数之前要做的''' r=func(*a ...
- 一篇分析诊断被"hang"住数据库的资料(Oracle Performance Diagnostic Guide——Hang/Locking)
该资料已上传至本人QQ群空间,如需该资料,可到本人QQ群空间查找.下面贴表文本: Oracle Performance Diagnostic GuideHang/LockingVersion 3.1. ...
- 快速切题 poj1573
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10708 Accepted: 5192 Des ...
- 快速切题 poj2632
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7799 Accepted: 3388 D ...