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 mn 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
思路:
拓扑排序裸题,用的是紫书上dfs的写法 实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = ;
int x,y,n,m,vis[M],topo[M],g[M][M],t;
bool dfs(int u){
vis[u] = -; //当前在判断的
for(int i = ;i <= n;i ++){
if(g[u][i]){
if(vis[i] < ) return false;
if(!vis[i]&&!dfs(i)) return false;
}
}
vis[u] = ; topo[t--] = u;
return true;
} bool toposort(){
t=n;
memset(vis,,sizeof(vis));
for(int i = ;i <= n;i ++)
if(!vis[i]&&!dfs(i)) return false;
return true;
} int main()
{
while(cin>>n>>m){
if(n==&&m==) break;
memset(g,,sizeof(g));
while(m--){
cin>>x>>y;
g[x][y] = ;
}
if(toposort()){
for(int i = ;i <= n;i ++){
if(i==n) cout<<topo[i]<<endl;
else cout<<topo[i]<<" ";
}
}
}
return ;
}

UVa 10305 - Ordering Tasks (拓扑排序裸题)的更多相关文章

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

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

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

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

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

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

  4. uva 10305 ordering tasks(超级烂题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm

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

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

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

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

  7. UVa 10305 Ordering Tasks (例题 6-15)

    传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==>  ...

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

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

  9. Ordering Tasks 拓扑排序

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

随机推荐

  1. 利尔达推出工控解决方式 串口转以太网模块LSD1ES-W5500_S2E0

    利尔达最近推出工控解决方式,串口转以太网模块LSD1ES-W5500_S2E0,模块基于WIZnet-W5500. 同一时候,这也是利尔达科技集团成为WIZnet代理商后,自行推出的第一款基于WIZn ...

  2. 【H5】滚动事件(jq)

    $(function(){ console.log($('html,body').scrollTop());  //记录滚动高度(滚动前) }) $('html,body').scroll(funct ...

  3. maven使用出现的错误

    修改mvn archetype:create  改成mvn archetype:generate 刚开始学习用Maven, 装好了以后生成一个新的project mvnarchetype:genera ...

  4. Spring集成Swagger,Java自动生成Api文档

    博主很懒... Swagger官网:http://swagger.io GitHub地址:https://github.com/swagger-api 官方注解文档:http://docs.swagg ...

  5. mfc 进程的优先级

    知识点:  进程优先级  获取当前进程句柄  优先级设置  优先级变动  优先级获取 一.进程优先级(优先级等级) 简单的说就是进程(线程)的优先级越高,那么就可以分占相对多的CPU时间片. ...

  6. 【LG4248】[AHOI2013]差异

    [LG4248][AHOI2013]差异 题面 洛谷 题解 后缀数组版做法戳我 我们将原串\(reverse\),根据后缀自动机的性质,两个后缀的\(lcp\)一定是我们在反串后两个前缀的\(lca\ ...

  7. oracle移动数据/修改数据文件路径

    参考:http://wwyz998.blog.163.com/blog/static/321867852011117111832334/ oracle移动数据文件 1.连接到数据库 [oracle@l ...

  8. 【python笔记】使用matplotlib,pylab进行python绘图

    一提到python绘图,matplotlib是不得不提的python最著名的绘图库,它里面包含了类似matlab的一整套绘图的API.因此,作为想要学习python绘图的童鞋们就得在自己的python ...

  9. selenium常用命令

    openopen(url)- 在浏览器中打开URL,可以接受相对和绝对路径两种形式type type(inputLocator, value)- 模拟人手的输入过程,往指定的input中输入值- 也适 ...

  10. LeetCode Merge k Sorted Lists (链表)

    题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...