拓扑排序-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的更多相关文章
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- 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 ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- 苹果Think Different广告
电视广告版: Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the s ...
- lightoj 1018 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...
- galera mysql 多主复制启动顺序及命令
Mysql 被复制机 sql启动
- c#基础语言编程-装箱和拆箱
引言 为什么有装箱和拆箱,两者起到什么作用?NET的所有类型都是由基类System.Object继承过来的,包括最常用的基础类型:int, byte, short,bool等等,就是说所有的事物都是对 ...
- AngularJS的开发工具---yeoman 简易安装
AngularJS 不错,yeoman作为推荐开发工具,网上的安装步骤较烦,这里给出简易步骤. 1.安装 Ruby 自己到 Ruby 官方下载最新安装包: http://rubyinstall ...
- 一个可视化的retrospective网站
IdeaBoardz - Brainstorm, Retrospect, Collaborate是一个可视化的retrospective,brainstorm的网站,比较简单易用,可以导出pdf和ex ...
- 20169210《Linux内核原理与分析》第二周作业
<Linux内核原理与分析>第二周作业 本周作业分为两部分:第一部分为观看学习视频并完成实验楼实验一:第二部分为看<Linux内核设计与实现>1.2.18章并安装配置内核. 第 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...
- 利用ssh传输文件 分类: 服务器搭建 Raspberry Pi 2015-04-12 18:47 58人阅读 评论(0) 收藏
在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www/local_dir(本 ...
- Linux Resin 安装
1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...