最近刷了几题拓扑排序的题,记录一下拓扑排序

在有向图中,并且按照一定的规则(题目所给的规则)排序。如果图中出现了有向环的话就无法排序了。

 int gap[maxn][maxn];//记录下有向边
int topo[maxn], c[maxn], t;//topo数组用来保存最后的排序结果,
//c数组用来判断是否有访问过或者成环
//t 用来记录当前topo数组的下标值
bool dfs(int u){
c[u] = -;//标记当前访问点
for(int v = ; v<=n; v++) if(gap[u][v]){
if(c[v]< ) return false;//形成有向回环,返回错误
else if(!c[v] && !dfs(v) ) return false;
}
c[u] = ;//标记访问完成
topo[--t] = u;//通过--t就可以完成从后往前保存数据(因为递归是从后往前)
return true;
}
bool toposort(){
t = n+;//t为当前的n个点数(n从0开始的话只要t = n,和将所有 <=n 换成 <n)
ms(c, );
for(int u = ; u<=n ;u++)
if(!c[u])
if(!dfs(u)) return false;
return true;
}

附上相关题目:1)http://codeforces.com/problemset/problem/510/C

Toposort(拓扑排序)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. hdu5438 拓扑排序+DFS

    解析 对一个有向无环图(Directed Acyclic Graph,简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则 ...

  4. HDU 5438 拓扑排序+DFS

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

  5. 拓扑排序-DFS

    拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...

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

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

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

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

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

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

  9. 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 ...

随机推荐

  1. Python笔记(二十一)_内置函数、内置方法

    内置函数 issubclass(class1,class2) 判断class1类是否为class2类的子类,返回True和False 注意1:类会被认为是自身的子类 >>>issub ...

  2. 【ABAP系列】SAP 的逻辑数据库解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 的逻辑数据库解析   前 ...

  3. LeetCode 114. Flatten Binary Tree to Linked List 动态演示

    把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...

  4. mglearn初探

    这个是取自于<python机器学习基础教程>16页 代码: # import numpy as np # import matplotlib.pyplot as plt # import ...

  5. git报错-Initial commit Untracked files nothing added to commit but untracked ……

    文章转自 https://www.jianshu.com/p/61c3db30d488 在目标执行命令 git stratus 报错 根据上面的文章,可以解决问题.不行的话,请留言. 感谢你的阅读

  6. ubuntu18.04 安装 jdk

    1.当前路径(如果不想下载到当前路径,可以先cd到指定路径,再开始下载),官网下载JDK文件jdk-8u121-linux-x64.tar.gz $ wget https://download.ora ...

  7. Python入门习题9.数码管时间

    #七段数码管.py import turtle,datetime def drawGap(): #绘制数码管间隔 turtle.penup() turtle.fd(5) def drawLine(dr ...

  8. #python# error:http.client.RemoteDisconnected: Remote end closed connection without response

    添加headers user-agent 网络情况不好的状态下也能出现

  9. Topcoder SRM653div2

    A . 250 Problem Statement      Some people are sitting in a row. Each person came here from some cou ...

  10. 20191114PHP验证码

    <?phpob_clean();$img=imagecreate(40,20);$back=imagecolorallocate($img,200,200,200); $blue=imageco ...