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两个任务 ...
随机推荐
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目4
2014-03-20 03:08 题目:给定一个集合,返回其幂集. 解法:DFS. 代码: // 9.4 Return all subsets of a set #include <cstdio ...
- CocosCreator设置启动场景
刚开始接触CocosCreator,在调试时,如果有多个场景,不知道如何设置将某个指定的场景设置为启动场景,折腾了一圈,找到了设置的地方, 记录一下. 点击项目->项目设置 在预览运 ...
- katalon系列一:初识Katalon Studio自动化测试工具
最近准备把公司的系统搞上UI自动化,先是自己用Python+selenium+pytest写了一个框架,开始写case的时候发现效率极其慢.原因为: (1)开发为提高前端响应时间,使用前端路由技术,一 ...
- 常用模块(time)
import time # data = time.time() # 获取时间戳# data = time.localtime() # 获取操作系统时间,也称本地时间,可传入时间戳# data = t ...
- selenium启动IE浏览器报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode mu
意思是浏览器的保护模式设置不一致所导致 解决方案-->修改IE设置 将所有区域的保护模式勾选去掉即可
- CandyCrush 糖果传奇
1.unity自带触发事件 unity的每一个Collider对象都有类似OnMouseDown.OnMouseOver等事件.此事件是存在于MonoBehaviour脚本里的,而MonoBehavi ...
- Singleton patterns 单件(创建型模式)
1.模式分类 1.1 从目的来看: • – 创建型(Creational)模式:负责对象创建. • – 结构型(Structural)模式:处理类与对象间的组合. • ...
- c语言版贪吃蛇小游戏
编译环境:windows 7 64位 编译工具:codeblocks 13.12 备注:未使用graphics.h 声明:个人原创,未经允许,禁止转载!!! 数据结构:双向链表 1.程序未使用grap ...
- 批量部署ssh免密登陆
#!/bin/bash#set -xservers="10.254.192.xx10.254.192.xx10.254.192.xx"passwd="xxxxxxxx&q ...
- 通过设计表快速了解sql语句中字段的含义
打开Navicat-------> 选择数据库 ------->右键设计表------>查看下方注释