[拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型:
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
题目大意
给出n 任务个数,m组任务关系(u,v),即先有u,才能有v,要求对所有任务进行排序,使得前面的任务应该先于后面的任务,输出一组解
本题有坑!见代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 4000
int c[maxn], topo[maxn], t, n, m;
bool G[maxn][maxn];
bool dfs(int u)
{
c[u] = -;
; v < n; v++)
{
if (G[u][v])
)
return false;
else if (!c[v])
dfs(v);
}
c[u] = ;
topo[--t] = u;
return true;
}
bool toposort()
{
t = n;
memset(c, , sizeof c);
; u < n; u++)
if (!c[u])
if (!dfs(u))
return false;
return true;
}
int main()
{
&& n)//因为m的值可能为0!
//while (scanf("%d%d", &n, &m) && m && n)
{
memset(G, , sizeof G);
int _u, _v;
while (m--)
scanf(][_v - ] = ;
if (toposort())
{
; i < n - ; i++)
printf();
printf(] + );
}
else
printf("No\n");
}
system("pause");
;
}
[拓扑排序]Ordering Tasks UVA - 10305的更多相关文章
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVa 10305 (拓扑排序) Ordering Tasks
题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...
- 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。
题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...
- Ordering Tasks UVA - 10305(拓扑排序)
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...
- 拓扑排序 (Ordering Tasks UVA - 10305)
题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- 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(拓扑排序)
题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...
随机推荐
- canvas图表详解系列(3):动态饼状图(原生Js仿echarts饼状图)
本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...
- 机器学习技法:01 Linear Support Vector Machine
Roadmap Course Introduction Large-Margin Separating Hyperplane Standard Large-Margin Problem Support ...
- Java IO(IO流)-2
IO流 第一部分 (OutputStreamWriter BufferOutputStream) 转换流 超类为Reader和Writer 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流 ...
- KindEditor文件上传成功前端显示上传失败
一.使用kindeditor 上传图片 ,根据kindeditor 要求返回了相应的数据, 但是kindeditor 插件显示上传失败!!! 解决方法: 各个版本位置可能不同!!! 1.修改kinde ...
- 【转】RAM 大全-DRAM, SRAM, SDRAM的关系与区别
http://blog.csdn.net/huleide/article/details/5506698 ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是R ...
- 【JDK1.8】JDK1.8集合源码阅读——HashMap
一.前言 笔者之前看过一篇关于jdk1.8的HashMap源码分析,作者对里面的解读很到位,将代码里关键的地方都说了一遍,值得推荐.笔者也会顺着他的顺序来阅读一遍,除了基础的方法外,添加了其他补充内容 ...
- spacemacs 初始安装报错
尝试使用emcas的配置文件spacemacs,第一次安装启动时,界面为纯白色,而且在输入完几个配置选项后,报了一个错误 Symbol's value as variable is void 根据官网 ...
- TinyOS编程思想和Nesc基础语法
TinyOS操作系统由nesc语言写成,从程序员角度看,它的基本作用就是提供了一组API接口以及一些编程规则. 具体来说,基于nesc语言的TinyOS编程行为具有以下特点: a.兼容C语言:使用ne ...
- 注解的形式与xml文件的形式完成事务管理及xml文件的配置
需要的jar包: c3p0-0.9.2.1.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1 ...
- angular高级篇之transclude使用详解
angular指令的transclude属性是一个让初学者比较难以理解的地方,transclude可以设置为false(默认),true或者对象三种值,如果不设该属性就默认为false,也就是说你不需 ...