拓扑排序的DFS算法

输入:一个有向图

输出:顶点的拓扑序列

具体流程:

(1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v]

(2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面

(3) 返回链表(按照链表次序输出顶点即为顶点的拓扑序列)

样例输入

5 5
0 1 1
0 2 1
1 2 1
2 3 1
4 2 1

样例输出

4 0 1 2 3


因为对有向无环图进行dfs遍历,得到的可能是森林,所以为了保证对所有点进行拓扑排序,我的做法是对所有入度为0的点进行dfs遍历

 #include <iostream>
#include <deque>
using namespace std; int ** edges;
int * visited;
int * in_degrees; int v,e;
deque<int> dq;
void dfs(int i){
visited[i] = ;
for(int j=;j<v;j++){
if(!visited[j] && edges[i][j] != ){
dfs(j);
}
}
dq.push_front(i);
} int main(void){
cin>>v>>e;
//init
edges = new int*[v];
visited = new int[v];
in_degrees = new int[v];
memset(visited,,v*sizeof(int));
memset(in_degrees,,v*sizeof(int)); int i;
for(i=;i<v;i++){
edges[i] = new int[v];
memset(edges[i],,v*sizeof(int));
} //input
for(i=;i<e;i++){
int a,b,w;
cin>>a>>b>>w;
edges[a][b] = w; //从a到b有一条路
in_degrees[b]++; //b的入度加1
} for(i=;i<v;i++){
if(==in_degrees[i]){//从入度为0的点进行dfs遍历
dfs(i);
}
}
while(!dq.empty()){
printf("%d ",dq.front());
dq.pop_front();
}
//huishou
for(i=;i<v;i++){
delete edges[i];
}
delete edges;
return ;
}

拓扑排序-DFS的更多相关文章

  1. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  2. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  3. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  4. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  5. POJ1128 Frame Stacking(拓扑排序+dfs)题解

    Description Consider the following 5 picture frames placed on an 9 x 8 array.  ........ ........ ... ...

  6. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  7. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  8. 拓扑排序/DFS HDOJ 4324 Triangle LOVE

    题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...

  9. CodeForces-1217D (拓扑排序/dfs 判环)

    题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...

随机推荐

  1. codeforce 230D Dijsktra

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  2. openStack 瓶颈测试

  3. 深入理解jvm之内存区域与内存溢出

    文章目录 1. Java内存区域与内存溢出异常 1.1. 运行时数据区域 1.1.1. 程序计数器 1.1.2. java虚拟机栈 1.1.3. 本地方法栈 1.1.4. Java堆(Java Hea ...

  4. MySQL 面试基础

    相关:http://blog.csdn.net/u013252072/article/details/52912385          http://blog.csdn.net/zhangliang ...

  5. 用python正则表达式提取字符串

    在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣的可以看re的教程. 提取一般分两种 ...

  6. JAVA IO详解

    [案例1]创建一个新文件 1 2 3 4 5 6 7 8 9 10 11 import java.io.*; class hello{     public static void main(Stri ...

  7. winform设置DataGridView样式 (蓝色)

    本文转载:http://www.cnblogs.com/hailexuexi/archive/2012/04/23/2466398.html 代码: #region DataGridVeiw Styl ...

  8. 史上最全WebView使用,附送Html5Activity一份

    本文来自:http://www.jianshu.com/users/320f9e8f7fc9/latest_articles感谢您的关注. WebView在现在的项目中使用的频率应该还是非常高的.我个 ...

  9. CentOS 6.7编译安装PHP 5.6

    1.准备编译环境 yum install gcc gcc-c++ pcre* openssl* gd-devel* libxml2-devel bzip2-devel libcurl-devel 2. ...

  10. Maven学习总结——聚合与继承

    一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模块一</module&g ...