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 ...
随机推荐
- CSS sprites(css 精灵):将小图标整合到一张图片上
一.什么是css sprites css sprites直译过来就是CSS精灵.通常被解释为“CSS图像拼合”或“CSS贴图定位”.其实就是通过将多个图片融合到一张图里面,然后通过CSS backgr ...
- LeetCode--121--卖卖股票的最佳时机
问题描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出 ...
- hdoj2476 String painter
题意:有一刷子,能将区间内涂成同一字母.给出src,dst串,问最少涂几次? 用dp[i][j]表示区间[i,j]内最少涂的次数.len=1,2时很明显.len=3时,dp[i][j]要么就在dp[i ...
- thinkphp条件查询
1.这是我在做项目的时候编写的: $profit = M('shipping_types',' ','DB_PROFIT');//没有表前缀,在M函数的第二个参数就为空. //条件$field = a ...
- 计时(.NET)
using System.Diagnostics; // 开始计时 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 要计时的操 ...
- RowMapper使用
public class ABRow implements RowMapper<AABB> { private String CMC; @Override public AABB mapR ...
- learning docker steps(2) ----- docker contailner 初次体验
参考:https://docs.docker-cn.com/get-started/part2/ Dockerfile的内容如下所示: # 将官方 Python 运行时用作父镜像 FROM pytho ...
- poj2892
题解: 答案=后缀-前缀-1 如果被轰了,那么就时0 在一开始加入0,n+1,保证有前缀后缀 代码: #include<cstdio> #include<cmath> #inc ...
- 关于edge detecte
1.注意render Texture 双击render Texture ,要勾选 use viewport dimensions,否则出现边框和模型不合的情况 2.注意edge detecte 的状态 ...
- c#版本与vs的对应关系
版本 .NET Framework版本 Visual Studio版本 发布日期 特性 C# 1.0 .NET Framework 1.0 Visual Studio .NET 2002 2002.1 ...