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两个任务 ...
随机推荐
- http客户端缓存
这篇文章写得比较,点击查看
- RPG游戏黑暗之光
1.设置默认鼠标光标 PlayerSettings → Default Cursor 下设置 2.为人物创建单一类 为人物创建了PlayerAnimation.cs.PlayerDir.cs.Play ...
- docker部署思路
1.docker安装2.拉取centos镜像或者Ubuntu镜像 看你用哪个3.使用镜像,run出来一个容器A4.进入容器A,安装uwsgi,把Django部署在下面5.在启动脚本中配置开机自启动脚本 ...
- PHP路径相关 dirname,realpath,__FILE__
比如:程序根目录在:E:\wamp\www 中 1. __FILE__ 当前文件的绝对路径 如果在index.php中调用 则返回 E:\wamp\www\index.php 下面再看一 ...
- 决策树与随机森林Adaboost算法
一. 决策树 决策树(Decision Tree)及其变种是另一类将输入空间分成不同的区域,每个区域有独立参数的算法.决策树分类算法是一种基于实例的归纳学习方法,它能从给定的无序的训练样本中,提炼出树 ...
- POJ 3177 Redundant Paths 无向图边双联通基础题
题意: 给一个无向图,保证任意两个点之间有两条完全不相同的路径 求至少加多少边才能实现 题解: 得先学会一波tarjan无向图 桥的定义是:删除这条边之后该图不联通 一条无向边(u,v)是桥,当且仅当 ...
- OAuth2.0 用户验证授权标准 理解
OAuth2.0是一套标准. 一.问题 这个标准解决了这样的一个问题. 允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. ...
- hust 1605 bfs
思路:直接用优先队列优化bfs. #include<map> #include<queue> #include<vector> #include<cmath& ...
- bzoj1266 [AHOI2006]上学路线route floyd建出最短路图+最小割
1266: [AHOI2006]上学路线route Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2490 Solved: 898[Submit][S ...
- 图表绘制工具--Matplotlib 1
''' [课程3.] Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python.numpy.pandas基本数据结构,运营高 ...