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)的更多相关文章

  1. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  2. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  3. Bzoj 1565: [NOI2009]植物大战僵尸 最大权闭合图,拓扑排序

    题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★   输入文件:pvz.in   输出文件:p ...

  4. BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP

    BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...

  5. BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序

    BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...

  6. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  7. 拓扑排序--UVa10305

    题目 Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortun ...

  8. poj 3683 2-sat建图+拓扑排序输出结果

    发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...

  9. 图->有向无环图->拓扑排序

    文字描述 关于有向无环图的基础定义: 一个无环的有向图称为有向无环图,简称DAG图(directed acycline graph).DAG图是一类较有向树更一般的特殊有向图. 举个例子说明有向无环图 ...

随机推荐

  1. linux系统root用户忘记密码的重置方法

    如果不小心忘记了新安装的lCentOS7的root密码,现在将找回过程分享给大家. 1.首先,在启动grub菜单,选择编辑选项启动: 2.然后,按e 进入编辑模式: 3.将'linux 16'行'ro ...

  2. windows server 2008 wamp安装报000F15A0解决方法

    wampserver2.2c-x64 原因:缺少Visual C++ 2008 Runtime x64,官网地址http://www.microsoft.com/zh-cn/download/deta ...

  3. HDU 1166 敌兵布阵 线段树单点更新求和

    题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...

  4. [图像]判断图片是PNG还是JPG格式

    typedef NS_ENUM(NSInteger, NSPUIImageType) { NSPUIImageType_JPEG, NSPUIImageType_PNG, NSPUIImageType ...

  5. 良心版Dolby Home Theater v4.1安装教程

    感(pi)谢(pan)一下两个教程: 文库文章链接:http://wenku.baidu.com/link?url=beBg_apvCuY3xiCXk4zl65Q7AmeCjoDGMol03K0xhk ...

  6. 【转载】写一个js库需要怎样的知识储备和技术程度?

    作者:小爝链接:https://www.zhihu.com/question/30274750/answer/118846177来源:知乎著作权归作者所有,转载请联系作者获得授权. 1,如何编写健壮的 ...

  7. .NET轻量级任务任务管理类

    概述 最近做项目总是遇到服务跑批等需求,一直想写个任务管理的DLL,现在整理了一下思路,编写了一个DLL类库,使用方便.只要调用的子类继承服务基类便可以实现任务的整体调度.先看看页面效果: 使用方式 ...

  8. Android学习资源整理

    官方文档:https://developer.android.com/guide/index.html (万万没想到居然有中文) 网友整理的学习笔记,挺不错的 http://www.runoob.co ...

  9. Code First数据库迁移

    生成数据库 修改类文件PortalContext.cs的静态构造函数,取消当数据库模型发生改变时删除当前数据库重建新数据库的设置. PortalContext() { Database.SetInit ...

  10. Arduino uno LED灯实验

    http://jingyan.baidu.com/article/a65957f4e358d924e67f9bad.html