HDU1285 裸的拓扑排序
拓扑排序:
拓扑排序是应用于有向无回路图(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 裸的拓扑排序的更多相关文章
- HDU1285 确定名次 拓扑排序
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
- hdu1285+hdu2467(拓扑排序)
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 拓扑排序 POJ2367Genealogical tree[topo-sort]
---恢复内容开始--- Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4875 A ...
- 拓扑排序--UVa10305
题目 Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortun ...
- UVA10305 拓扑排序
网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...
- [poj2585]Window Pains_拓扑排序
Window Pains poj-2585 题目大意:给出一个4*4的方格表,由9种数字组成.其中,每一种数字只会出现在特定的位置,后出现的数字会覆盖之前在当前方格表内出现的.询问当前给出的方格表是否 ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- 拓扑排序 Topological Sort
2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...
随机推荐
- FreeBSD--网络配置
配置网口:ifconfig |less dc0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 i ...
- tcl之控制流-条件运算、条件测试、逻辑表达
- 45.VUE学习之--组件之父组件使用scope定义子组件模板样式结构实例讲解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 图解HTTP总结(7)——确保Web安全的HTTPS
HTTP 主要有这些不足, 例举如下. 通信使用明文( 不加密) , 内容可能会被窃听. 不验证通信方的身份, 因此有可能遭遇伪装. 无法证明报文的完整性, 所以有可能已遭篡改 ...
- iOS-修改modal出来的控制器的大小
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ static BOOL sh ...
- ### Cause: java.lang.reflect.UndeclaredThrowableException
### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...
- gcc常用命令
1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...
- android onCreate的两个方法
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { } override ...
- 学习网络请求返回json对应的model
原来泛型可以这样用: 网络返回基类,返回一个code,msg,body,其中body不确定,所以,我们把它写成泛型 import org.json.JSONObject; /** * 网络请求的基类 ...
- Java 基本数据类型总结一
Java基本数据类型总结一 基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型.java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变 ...