拓扑排序:

拓扑排序是应用于有向无回路图(DAG)上的一种排序方式,对一个有向无回路进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u在v的前面。该序列说明了顶点表示的事件或 状态发生的整体顺序。

比较经典的是在工程活动上,某些工程完成后,另一些工程才能继续,此时可以以工程为顶点,工程间的依赖关系为边建立图,用拓扑排序来求得所有工程的合理执行顺序。

对一个DAG进行拓扑排序有两种方法,广度优先搜索和深度优先搜索

这里介绍广度优先搜索,进行拓扑排序时,每次可以拿出的顶点一定是入度为0的点,即没有被指向的点,因为这样的点表示的事件没有依赖,在一个入度为0的点表示的事件执行完之后,它所指向的顶点所依赖的点就少了一个,所以我们可以先将所有入度为0的点加入一个队列中,然后依次将它们所指向的点的入度减1,再将入度变为0的点也依次加入队列中,这样最后就可以得到一个拓扑有序的序列。

本题中说符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前,需要用到优先队列,每次从队列中取的是最小的那个元素

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=;
int graph[maxn][maxn];//保存图
int degree[maxn];//保存入度 int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(graph,,sizeof(graph));
memset(degree,,sizeof(degree));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(!graph[u][v])
{
graph[u][v]=;
degree[v]++;//v的入度++
}
}
priority_queue<int,vector<int>,greater<int> >q;
for(int i=;i<=n;i++)
if(degree[i]==)
q.push(i);
bool first=;
while(!q.empty())
{
int cur=q.top();
q.pop();
if(first)
{
cout<<cur;
first=;
}
else
cout<<" "<<cur;
for(int i=;i<=n;i++)
{
if(graph[cur][i])
{
degree[i]--;//相连的点的入度减1
if(degree[i]==)//如果入度为0,加入队列
q.push(i);
}
}
}
printf("\n");
}
return ;
}
/**
* The Kahn's Topological Sort Algorithm in C++
* Using the Adjecency List
* Time Cost : O(|V|+|E|)
* Author: Zheng Chen / Arclabs001
* Copyright 2015 Xi'an University of Posts & Telecommunications
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std; const int N = ; // The number of Vertex enum status {UNDISCOVERED,VISITED}; struct Vertex
{
int inDegree, outDegree;
int data;
status _stat;
}V[N]; vector<int> AdjList[N]; //Using vector to simulate the adjlist
queue<int> vertexQueue; //The call queue
/**
* Initialize the graph as below:
The Graph: 0->1->3
| | |
\/ \/ \/
2->4<-- * @return The pointer to the start vertex
*/
Vertex* init_graph()
{
while(!vertexQueue.empty())
vertexQueue.pop(); for(int i=; i<N; i++)
{
AdjList[i].clear();
V[i]._stat = UNDISCOVERED;
V[i].data = i;
} V[].inDegree = ; V[].outDegree = ;
V[].inDegree = ; V[].outDegree = ;
V[].inDegree = ; V[].outDegree = ;
V[].inDegree = ; V[].outDegree = ;
V[].inDegree = ; V[].outDegree = ; AdjList[].push_back(); AdjList[].push_back();
AdjList[].push_back(); AdjList[].push_back();
AdjList[].push_back();
AdjList[].push_back(); return & V[];
} bool Topological_Sort()
{
for(int i=; i<N; i++)
{
if(V[i].inDegree == )
vertexQueue.push(i);
} while(!vertexQueue.empty())
{
int top = vertexQueue.front();
V[top].outDegree = ;
V[top]._stat = VISITED;
int i=; for(int v : AdjList[top])
{
--V[v].inDegree; AdjList[top][i++] = -;
if(V[v].inDegree == )
vertexQueue.push(v);
}
cout<<top<<" "; vertexQueue.pop();
} for(int i=; i<N; i++)
{
for(int v : AdjList[i])
if(v != -)
{
return false;
}
} return true;
} int main()
{
init_graph(); bool status = Topological_Sort();
if(status == false)
{
cout<<"Error! The graph has at least one cycle!"<<endl;
}
return ;
}

HDU1285 裸的拓扑排序的更多相关文章

  1. HDU1285 确定名次 拓扑排序

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  2. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  3. hdu1285+hdu2467(拓扑排序)

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...

  5. 拓扑排序--UVa10305

    题目 Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortun ...

  6. UVA10305 拓扑排序

    网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...

  7. [poj2585]Window Pains_拓扑排序

    Window Pains poj-2585 题目大意:给出一个4*4的方格表,由9种数字组成.其中,每一种数字只会出现在特定的位置,后出现的数字会覆盖之前在当前方格表内出现的.询问当前给出的方格表是否 ...

  8. CF-825E Minimal Labels 反向拓扑排序

    http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...

  9. 拓扑排序 Topological Sort

    2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...

随机推荐

  1. python系列4之装饰器

    目录 递归算法解析 冒泡排序解析 装饰器解析 一. 递归 1. 递归的定义 递归(Recursion),又成为递回,在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.递归一词还较长用于描述以 ...

  2. POJ 2836 状压DP

    Rectangular Covering Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2727   Accepted: 7 ...

  3. C++ 整型长度的获取 不同的系统

    不同的系统中,C++整型变量中的长度位数是不同的,通常,在老式的IBM PC中,int 的位数为16位(与short相同),而在WINDOWS XP,Win7,vax等很多其他的微型计算机中,为32位 ...

  4. 输入cin对象的用法

    #include<iostream> using namespace std; int main() { int carrots ; cout << "How man ...

  5. Hive 压缩技术Data Compression

    Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...

  6. JVM——九大工具助你玩转Java性能优化

    本文转载自 http://www.importnew.com/12324.html 本文由 ImportNew - 陈 晓舜 翻译自 idrsolutions.欢迎加入翻译小组.转载请参见文章末尾的要 ...

  7. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  8. 树莓派Raspberry Pi 3安装步骤

    一.需要的硬件 1.Raspberry Pi 3(Model B+)树莓派.购买>https://item.jd.com/29225467867.html 2.输出5V/2A的电源 3.SD卡( ...

  9. 【Adaptive Boosting】林轩田机器学习技法

    首先用一个形象的例子来说明AdaBoost的过程: 1. 每次产生一个弱的分类器,把本轮错的样本增加权重丢入下一轮 2. 下一轮对上一轮分错的样本再加重学习,获得另一个弱分类器 经过T轮之后,学得了T ...

  10. python+selenium安装指导

    一.      安装python 1.Window 平台安装 Python 以下为在 Window 平台上安装 Python 的简单步骤: 打开 WEB 浏览器访问https://www.python ...