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. 今天升级Xcode 7.0 bata发现网络访问失败。

    今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...

  2. hdu2829 四边形优化dp

    Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. Step by Step 使用HTML5开发一个星际大战游戏(1)

    本系列博文翻译自以下文章 http://blog.sklambert.com/html5-canvas-game-panning-a-background/ Languages: HTML5, Jav ...

  4. OpenGL ES2.0编程三步曲 -转

    原地址:http://blog.csdn.net/myarrow/article/details/7707943 1. 保存全局变量的数据结构 以下例子程序均基于Linux平台. typedef st ...

  5. ActiveX控件在项目中的应用

  6. "0" 并不一定是 假 (false)

    写习惯C/C++系代码的人应该很习惯看见类似这样的代码: 1 2 3 4 5 int i = 0; ...... if(i){    //这里代码不会被执行 } 因此写习惯以后会想当然地觉得其他语言里 ...

  7. UVA-10603-Fill(BFS+优先队列)

    There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not grea ...

  8. Python扫描指定文件夹下(包含子文件夹)的文件

    扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...

  9. 基于Spark机器学习和实时流计算的智能推荐系统

    概要: 随着电子商务的高速发展和普及应用,个性化推荐的推荐系统已成为一个重要研究领域. 个性化推荐算法是推荐系统中最核心的技术,在很大程度上决定了电子商务推荐系统性能的优劣,决定着是否能够推荐用户真正 ...

  10. Kafka 集群搭建 (自用)

    Zookeeper集群搭建 1.软件环境 (3台服务器-测试环境) 192.168.56.9 192.168.56.6 192.168.56.7 1.Linux服务器一台.三台.五台.(2*n+1), ...