本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html

题意:

假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v。那么。所有变量从小到大排列起来应该是什么样子的呢?例如,有四个变量a,b,c,d,若a < b, c < b, d < c, 则这四个变量的排序可能是a < d < c < b;尽管还有其他可能,你只需要找出其中一个即可。

思路:

把每个变量看成一个点,“小于”看成一个边,则得到一个有向图。这样,实际任务就是把一个图的所有节点排序,使得对应的每一条有向边(u, v),对应的u都排在v前面,即就是拓扑排序。

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std; const int maxV = ;
int head[maxV + ];
int n, m;
queue<int> ansQu; struct EdgeNode{
int to;
int next;
}Edges[maxV * maxV + ]; int indegree[maxV]; void getIdg()//获得入度
{
memset(indegree, , sizeof(indegree));
for(int i = ; i <= m; i++)
indegree[ Edges[i].to ]++;
} void tplgSort()//拓扑排序
{
getIdg();
stack<int> tpst;
for(int i = ; i <= n; i++)
if(!indegree[i]) tpst.push(i);
while(!tpst.empty())
{
int v = tpst.top();
ansQu.push(v);
tpst.pop();
for(int j = head[v]; j != -; j = Edges[j].next)
if(!(--indegree[ Edges[j].to ]))
tpst.push(Edges[j].to);
}
} int main()
{
while(~scanf("%d%d", &n ,&m) && (n || m))
{
memset(head, -, sizeof(head));
memset(&Edges, , sizeof(EdgeNode));
for(int i = ; i <= m; i++)
{
int u, v;
scanf("%d%d",&u, &v);
Edges[i].to = v;
Edges[i].next = head[u];
head[u] = i;
}
tplgSort();
int flag = ;
while(!ansQu.empty())
{
printf(flag++ ? " %d":"%d", ansQu.front());
ansQu.pop();
}
printf("\n");
}
return ;
}

UVA10305 Ordering Tasks (拓扑序列)的更多相关文章

  1. 拓扑排序(Topological Order)UVa10305 Ordering Tasks

    2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...

  2. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  3. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  4. M - Ordering Tasks(拓扑排序)

    M - Ordering Tasks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descri ...

  5. UVA-10305 Ordering Tasks (拓扑排序)

    题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...

  6. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  7. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  9. Uva10305 Ordering Tasks

    John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...

随机推荐

  1. ul中li元素横向排列且不换行

    ul { white-space: nowrap; } li { display: inline-block; }     white-space 属性设置如何处理元素内的空白. normal 默认. ...

  2. Visual C++ 图像处理类库CxImage源代码

    说明:VC++ 图像处理类库CxImage源代码,CxImage是一个可以用于MFC 的C++类,可以打开,保存,显示,转换各种格式的图像文件,比如BMP, JPEG, GIF, PNG, TIFF, ...

  3. CodeSimth - .Net Framework Data Provider 可能没有安装。解决方法[转载 ]

    原文:http://www.cnblogs.com/chenrui7/p/3592082.html 今天想使用CodeSimth生成一个sqlite数据库的模板.当添加添加数据库的时候发现: .Net ...

  4. mssql发布订阅事项

    在发布订阅过程中遇到2个需求: 1.在原有的发布快照中增加发布内容,追加模式需要在追加的表中设计一个主键,要不然没有办法进行发布的,另外还得注意将这两个字段进行更改: select immediate ...

  5. SpringMVC<一> 基本结构与配置

    刚刚踏入SpringMVC的学习,有一定Strust2的使用经验,边看书看博客,边总结,如有不对的地方还希望各位大佬多多指正. Spring 响应过程与结构 (1)用户在客户端发送一个HTTP请求,W ...

  6. 软件架构---.net框架介绍

    https://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生 ...

  7. kibana的查询语法

    kibana的查询语法是    字段Fields:关键词

  8. Linux下nginx支持.htaccess文件实现伪静态的方法!

    在Google上搜索的资料很多人都说nginx目前不支持.htaccess文件,我按照nginx的规则试验了一下,结果发现nginx是完全支持.htaccess文件的! 方法如下: 1. 在需要使用. ...

  9. 啥叫sched-domain

    这周问过公司里专家,说cpu-load是说CPU的计算能力,但是从代码实在不知道cpu-load说的是啥! SD_SHARE_CPUPOWER 0X8000  domain的成员共享cpu power ...

  10. AJAX提交表单后要清空,否则再次提交原来的数据会认为重复提交,提交失败。使用ajaxSubmit 函数需要引入jquery.form.min.js 文件

    <script src="../../Scripts/js/jquery.form.min.js" type="text/javascript">& ...