UVA10305 Ordering Tasks (拓扑序列)
本文链接: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 (拓扑序列)的更多相关文章
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- M - Ordering Tasks(拓扑排序)
M - Ordering Tasks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- UVA-10305 Ordering Tasks (拓扑排序)
题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Ordering Tasks 拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- Uva10305 Ordering Tasks
John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...
随机推荐
- 修改window 10 开始菜单问题
cmd->powershell Get-AppxPackage | % { Add-AppxPackage -DisableDevelopmentMode -Register "$($ ...
- 【APUE】Chapter11 Threads
看完了APUE第三版的Chapter11 Threads,跟着书上的demo走了一遍,并且参考了这个blog(http://www.cnblogs.com/chuyuhuashi/p/4447817. ...
- Linux/Unix中系统级IO
Linux/unix I/O:将设备映射为文件的方式,允许Unix内核引出一个简单.低级的应用接口. Linux/unix IO的系统调用函数很简单,它只有5个函数:open(打开).close(关闭 ...
- Percona-Tookit工具包之pt-duplicate-key-checker
Preface I suppose that we have a requirement of checking out how many duplicated indexes on ...
- python 学习分享-rabbitmq
一.RabbitMQ 消息队列介绍 RabbitMQ也是消息队列,那RabbitMQ和之前python的Queue有什么区别么? py 消息队列: 线程 queue(同一进程下线程之间进行交互) 进程 ...
- python 学习分享-线程
多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...
- leetcode 174. 地下城游戏 解题报告
leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...
- 第九章 广播和本地组播(IGMP和MLD)
距离项目开启已经过去了一段时间,这段时间内自己学习的内容也算挺多的,但是也较容易遗忘,之后应该在空余的时间内多翻翻博客,更加清楚传统计算机网络的运作. 由于51要出去玩,更要好好利用好最近的时间.完成 ...
- LeetCode -- Construct Binary Tree from Preorder and Inorder
Question: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may as ...
- [hdu6432]Problem G. Cyclic
题目大意:给你$n$,一种合法的排列为,排列中没有$s[i\%n+1]-s[i]==1$,求合法方案数 题解:容斥,令$f_{i,j}$表示有$i$个元素,至少包含$j$个$s[i\%n+1]-s[i ...