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. 爬取维基百科人物介绍,并使用pymysql存储到数据库

    代码如下: from urllib.request import urlopen from bs4 import BeautifulSoup import re import datetime imp ...

  2. Android开发工具

    Android开发工具: AndroidDevTools: 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. ...

  3. node.js学习一——什么是node.js

    定义:node.js是运行在服务器端的运用了谷歌v8引擎的javascript运行平台 特点:1. 异步式I/O(非阻塞式I/O) 2. 事件驱动 什么是异步式I/O(非阻塞式I/O)? 要了解什么是 ...

  4. 网络抓包工具Wireshark和Fidder

    http://fangxin.blog.51cto.com/1125131/735178 http://blog.csdn.net/jiangwei0910410003/article/details ...

  5. 连接sqlexpress

    sqlexpress在visualstudio安装时可选择安装. 数据源添加 localhost\sqlexpress window身份认证即可.

  6. Idea下Android的配置

    (1) 下载安装好Intellij Idea和Android SDK. (2) Android SDK设置 ,在FIle –> Other Settings –> Default Proj ...

  7. springBoot框架不同环境读取不同的配置文件

    1. 3个配置文件(更多环境可以建多个): application.properties  (公共配置文件) application-dev.properties  (开发环境) applicatio ...

  8. 一起來玩鳥 Starling Framework(4)TouchEvent,Touch,以及TouchPhase

    這一篇來介紹一下TouchEvent.我們先來談單點的touch,下一篇再介紹MultiTouch.翻過Starling文件的應該會發現,Starling裡面沒有MouseEvent,而是整合在Tou ...

  9. Shell实现循环执行curl向Solr导入json文件

    #!/bin/bash for file in ./文件夹名/* do echo $file curl "http://IP:8983/solr/集合名/update?commit=true ...

  10. HTML5 Canvas 绘制库存变化折线 计算出库存周转率

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...