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. ceph状态信息靠谱查询

    1)检查集群的状态汇总信息: [root@haha1 clouder]# ceph -s cluster 8e136e25-77ab-4e0b-b24b-232a7b466cfe health HEA ...

  2. Debian 鼠标左右手

    环境:debian testing;xfce4桌面 在debian中想把鼠标改为左手操作,在设置中调整鼠标的按钮为左撇子根本没用!网上搜索后发现事实很简单,简单到不知该怎么说. 废话少说,放码过来. ...

  3. 【转】python直接运行tcl脚本

    python中调用tcl是通过加载TkInter来实现的. from Tkinter import Tcl tcl = Tcl() tcl.eval('source tu.tcl') tcl.eval ...

  4. 【H5】移动端页面根font-size设置

    h5rem.js   (配置写法①) (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationc ...

  5. 实测—fft IP核使用(包括ifft的配置使用)

    Vivado xilinx fft9.0 使用笔记: ****注 仿真实测1024点的转换需要经过1148个时钟周期才能得到转换结果: 模块配置信号含义请参考pg109文档手册(写的贼烂会看晕),不详 ...

  6. 20155226 Exp2 后门原理与实践

    20155226 Exp2 后门原理与实践 第一次实验博客交了蓝墨云未在博客园提交,链接 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机 ...

  7. 【php增删改查实例】第八节 - 部门管理模块(编写PHP程序)

    首先,在同级目录新建一个query.php文件: 接着,去刷新页面,打开F12,NetWork,看看当前的请求能不能走到对应的php文件? 这就说明datagrid确实能够访问到query.php 只 ...

  8. Unity关于方法事件生命周期官方文档

    http://docs.unity3d.com/Manual/ExecutionOrder.html 一.组件运行的基本顺序 下图中创建类的顺序为A,B,C,A1,二运行的结果为A1,B,C,A. 可 ...

  9. 如何使用URLOS进行docker应用开发

    使用Docker技术可以帮助企业快速水平扩展服务,从而到达弹性部署业务的能力.在云服务概念兴起之后,Docker的使用场景和范围进一步发展,如今在微服务架构越来越流行的情况下,微服务+Docker的完 ...

  10. Windows下fabric sdk连接Linux上fabric网络的调试过程

    上个月刚入职一家公司从事区块链研发工作,选型采用Hyperledger Fabric作为开发平台.团队的小组成员全部采用的是在VirtualBox上面安装桌面版的Ubuntu 16.04虚拟机,开发工 ...