图——拓扑排序(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图是一类较有向树更一般的特殊有向图. 举个例子说明有向无环图 ...
随机推荐
- printf对齐
C语言中,将printf函数打印出的字符像表格一样分类对齐.%-10d表示这个字符型占10个字节,负号表示左对齐.即下面表格中的x1位置开始填充.如果是%10d,表示右对齐,即在x10位置对齐. x1 ...
- OpenGL中坐标系的理解(一)
在OpenGL中,存在着至少存在着三种矩阵,对应着函数glMatrixMode()的三个参数:GL_MODELVIEW,GL_PROJECTION,GL_TEXTURE. 以下主要描述GL_MODEL ...
- JDBC的操作总结
JDBC 操作总结 JDBC是一组能够执行SQL语句的API JDBC的操作方式比较单一,简单的分为以下几个流程: 1.通过数据库厂商提供的JDB类库想DriverManager注册数据库驱动 ...
- List提取相同元素
List<int> currentList = Cls_Data.SoruceDataIntses[key]; preList = currentList.Intersect(preLis ...
- Struts2中Date日期转换的问题
今天跑程序的时候莫名其妙的出现了下面的一个异常: java.lang.NoSuchMethodException:com.ca.agent.model.mybatis.ApprovalInforC ...
- ArrayList、Vector、LinkedList源码
List接口的一些列实现中,最常用最重要的就是这三个:ArrayList.Vector.LinkedList.这里我就基于JDK1.7来看一下源码. public class ArrayList< ...
- CENTOS7 mysql 安装
CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 $ wget http://repo.mysql.com ...
- windows C++实现注销、重启、关机 logoff reboot shutdown
实现这一功能很简单,主要需要调用一个系统API ExitWindowsEx 功能就是,注销当前用户,关闭系统,或者重新启动系统. 它会发送一个WM_QUERYENDSESSION消息给所有的应用程序, ...
- 【React】组件生命周期
初始化阶段 getDefaultPropos:只调用一次,实力之间共享引用 getInitialState:初始化每个实例特有的状态 componentWillMount:render之前最后一次修改 ...
- Win7 U盘安装Ubuntu16.04 双系统详细教程
Win7 U盘安装Ubuntu16.04 双系统详细教程 安装主要分为以下几步: 一. 下载Ubuntu 16.04镜像软件: 二. 制作U盘启动盘使用ultraISO: 三. 安装Ubuntu系统: ...