图——拓扑排序(uva10305)
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
题意:就是给你几组任务,给出了任务的先后顺序,求出他们的执行顺序
拓扑排序就是用来解决这类排序问题的... 先构造出一个图,用dfs递归实现来遍历
自己敲的时候,wa了n次...然后Baidu了一下,发现自己的错误和网友的错误惊人的相似(QAQ),判断m n是否为0的时候应该用 m||n 来确定两个都为0而不是 m&&n !!
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn = + ; int a[maxn][maxn];
int c[maxn];
int topo[maxn];
int n, m, t; bool dfs(int u){
c[u] = -;
for(int v=; v<=n; v++){
if(a[u][v]){
if(c[v] < ) return false;
else if(!c[v] && !dfs(v)) return false;
}
}
c[u] = ;
topo[--t] = u;
return true;
}
bool topo_sort(){
t = n;
memset(c, , sizeof(c));
for(int u=; u<=n; u++){
if(!c[u]){
if(!dfs(u)) return false;
}
}
return true;
} int main(){
int j, k;
while(scanf("%d %d", &n, &m) == && (n || m )){
memset(a, , sizeof(a));
for(int i=; i<m; i++){
cin >> j >> k;
a[j][k] = ;
}
if(topo_sort()){
for(int i=; i<n; i++){
if(i == ) cout << topo[i];
else cout << " " << topo[i];
}
cout << endl;
}
else
cout << "No" << endl; } return ;
}
图——拓扑排序(uva10305)的更多相关文章
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- Bzoj 1565: [NOI2009]植物大战僵尸 最大权闭合图,拓扑排序
题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:p ...
- BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP
BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...
- BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- 拓扑排序--UVa10305
题目 Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortun ...
- poj 3683 2-sat建图+拓扑排序输出结果
发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...
- 图->有向无环图->拓扑排序
文字描述 关于有向无环图的基础定义: 一个无环的有向图称为有向无环图,简称DAG图(directed acycline graph).DAG图是一类较有向树更一般的特殊有向图. 举个例子说明有向无环图 ...
随机推荐
- swift错误 Expressions are not allowed at the top level
``` ... earlier we said top-level code isn't allowed in most of your app's source files. The excepti ...
- MySql触发器语法总结
经过昨天多次失败,今天终于将我要实现的触发器功能写成功了,触发器代码如下: -- use dbfortest; drop trigger if exists tg_before_insert_on_d ...
- quertz 使用小例
最近工作中用了个任务调度,学的不好,就把它记下来,为了以后参考 一般的 quartz都允许有一个名为quartz.properties的配置文件,通过它可以修改quartz框架运行时的环境: Quar ...
- 【转】Js获取当前日期时间及格式化操作
(转自:http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800002.html) var myDate = new Date(); myD ...
- UML大战需求分析——阅读笔记03
读<UML大战需求分析>有感03 状态机图和活动图在样子比较相似,但状态机图是用来为对象的状态及造成状态改变的事件建模.我们大二学习UML统一建模语言状态机图模块时了解到,UML的状态机图 ...
- ShareSDK分享失败的原因
关于分享估计很多都用的是ShareSDK的社会化分享,简单方便,支持的种类很多,但是一般的话都还是QQ,微信,新浪微博,腾讯微博为主. 最近需要导入一个分享的模块,失败了几次之后最终成功,分享给大家, ...
- tp框架之模板继承
模板继承是一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类的继承一样,模板也可以定义一个基础模板(或者是布局),并且其中定义相关的区 ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 你不知道的CSS背景—css背景属性全解
CSS背景在网页设计中使用频率非常高,然而对于这个开发人员很熟悉的CSS属性,却隐藏着许多不为初级开发人员熟知的细节,这篇文章尝试扒开这层不为人知的面纱. 首先列举一下CSS中关于元素背景的所有属性并 ...
- 【转载】VS2012的打包方法
上篇博客把收费系统的总体设计进行了一遍讲解,讲解的同时掺杂了些有关.NET编译机制的总结.程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的是没有了打包工具.不知道出于什么原因 ...