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 nish 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
 
/**
题目:Ordering Tasks UVA - 10305
链接:https://vjudge.net/problem/UVA-10305
题意:给定一个有向无环图,求拓扑序列。
思路:
对一条链,从后往前存入到数组的头部。
如:5->4->3->2; 那么存到数组为: a[] = {5,4,3,2}; 其他链要么和这条链尾部有交集,其他没有交集。由于交集处已经存进去了,为交集存入的肯定是放在数组的头部。
这样可以保证。 如果存在环,不存在拓扑序列。
*/ #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e2+;
const double eps = 1e-;
int topo[maxn], c[maxn], z;
int G[maxn][maxn];
int n, m;
bool dfs(int u)
{
c[u] = -;
for(int i = ; i <= n; i++){
if(G[u][i]==) continue;
if(c[i]==-) return false;///如果存在环,那么返回false;因为-1表示这条链还没寻找结束,
///如果一直寻找,找到了原来出现过的,那么存在环。
if(c[i]==&&G[u][i]){
if(dfs(i)==false) return false;
}
}
topo[z-] = u;
c[u] = ;
z--;
return true;
}
bool topoSort()
{
memset(c, , sizeof c);
z = n;
for(int i = ; i <= n; i++){
if(c[i]==){
if(dfs(i)==) return false;
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)==&&(n+m))
{
int u, v;
memset(G, , sizeof G);
for(int i = ; i < m; i++){
scanf("%d%d",&u,&v);
G[u][v] = ;
}
topoSort();
printf("%d",topo[]);
for(int i = ; i < n; i++){
printf(" %d",topo[i]);
}
printf("\n");
}
return ;
}

Ordering Tasks UVA - 10305 图的拓扑排序的更多相关文章

  1. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

  2. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  3. Ordering Tasks UVA - 10305(拓扑排序)

    在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...

  4. 拓扑排序 (Ordering Tasks UVA - 10305)

    题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...

  5. 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序

    题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆.  现在 ...

  6. 算法87-----DAG有向无环图的拓扑排序

    一.题目:课程排表---210 课程表上有一些课,是必须有修学分的先后顺序的,必须要求在上完某些课的情况下才能上下一门.问是否有方案修完所有的课程?如果有的话请返回其中一个符合要求的路径,否则返回[] ...

  7. UVA 1572 Self-Assembly(拓扑排序)

    1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...

  8. Paint the Grid Again (隐藏建图+优先队列+拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  9. 图的拓扑排序,AOV,完整实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

随机推荐

  1. Swift中获取相册图片与保存到相册

    关于这个网上目前位置记录的资料比较少,记录一下这个坑 获取相册图片 1: var iPC = UIImagePickerController() 2: iPC.sourceType = UIImage ...

  2. 正则表达式之 /^(\d)$/

    ^(\d)$就是0-9的任意一个数字, ^表示以...开头,\d表示0-9的数字,$表示以...结尾,

  3. 腾讯云linux服务器分区方案

    刚刚在腾讯云买了一台服务器,刚买的服务器的数据盘都是需要自己来分区的,下面就记录一下操作. 通过命令fdisk-l查看硬盘信息 可以看到有两块硬盘/dev/vda和/dev/vdb,启动vda是系统盘 ...

  4. dubbo发布webservice服务

    dubbo发布webservice服务 学习了:https://blog.csdn.net/zhangyunpengchang/article/details/51567127 https://blo ...

  5. mybatis常用jdbcType数据类型以及对应的JavaType

    1.MyBatis 通过包含的jdbcType类型 BIT.FLOAT.CHAR .TIMESTAMP . OTHER .UNDEFINEDTINYINT .REAL .VARCHAR .BINARY ...

  6. java 入门-helloWorld

    Java 教程 Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. ...

  7. python——3.x的异常处理 请注意了

    以前的python2.x的时候:try:        fp=urllib.request.urlopen(blogurl)    except Exception, e:        print  ...

  8. oracle新建一个表空间和用户来測试

    首先对表空间作例如以下说明 暂时表空间:是在做大数据量排序时.分组操作时用的.正常这些都是在内存中完毕的.但在大数据量排序处理时.内存不够用的情况下就会用到暂时表空间,这里是不存放表的,有点类似于操作 ...

  9. js获取本页的来源地址

    document.referrer 该属性可以获取到文档的载入地址 需要注意必须是通过改变localtion的href属性或a标签跳转才能获取到 否者将获取到空字符串

  10. C语言学习笔记(三) 输入输出函数的基本用法以及运算符

    printf() ——将内容输出到显示器上 四种用法 1.printf("字符串");   直接输出字符串 2.printf("输出控制符",输出参数); 3. ...