UVA10305:Ordering Tasks(拓扑排序)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.
John有n件工作要做,不幸的是这些工作并不独立,而是某件工作需要在另一件已经执行完了以后才能执行。
Input
The input will consist of several instances of the problem. Each instance begins with a line containing two integers, 1 ≤ n ≤ 100 and m. n is the number of tasks (numbered from 1 to n) and m is the number of direct precedence relations between tasks. After this, there will be m lines with two integers i and j, representing the fact that task i must be executed before task j. An instance with n = m = 0 will finish the input.
输入会包括若干组。每组先输入 n([1,100])和 m,其中 n 代表标号为 1~n 的 n 件工作。接下来的 m 行给出工作之间的优先度,每行给出两个整数 i、j,代表 i 会在 j 之前执行,n = m = 0时结束输入。
Output
For each instance, print a line with n integers representing the tasks in a possible order of execution.
对于每组数据,打印n个整数,代表着一种可能出现的执行顺序(意即多解输出某一解即可)。
Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3
介绍:
把每个变量看成一个点,“小于”关系看成有向边(比如输入1 2,我们可以画箭头1-->2),这样就可以把数据转化为一个有向图。把图的所有结点排序使得每一条有向边(u,v)对应的u都排在v的前边(不一定相邻)。在图论中,这个问题称为拓扑排序。
本题思路:
显然,我们无法从前到后去贪心选取路径,比如用样例来讲,如果我们先搜索到了 1->3 这条路,然后就储存在结果上的话,无法得知是否还有 1->2->3 这个限定路径,之后再搜到 2 这个点也没法往里补,而 5 这个点处在哪里也不好写命令。
所以反过来贪心:可以发现当深搜到最底端到达点 3 时,它后面再也没有点了,那么无论如何处置其他的点,3放在最后总是没错的。而为了得出点 1 和点 2 的顺序,可以在某个点for遍历了它的全部出度并深搜以后,再将此点放入拓扑序的前端。比如点 1 先扫描到了点 3,到头了,3放里;然后点 1 还有个指向点 2 的箭头,再dfs点 2,于是点 2 也放里了;然后点 1 遍历结束,点 1 放里……请用代码细细体会。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = ;
int n, m;
int topo[maxn], temp;//最终的拓扑序
bool book[maxn];//记录是否已经访问过某点
vector <int> chu[maxn];//chu[i]储存i指向的所有点 void dfs(int cur)
{
for (int i : chu[cur])
//C++11的特性,表示遍历了vector(实际上哪个容器都可以这么用),i代表具体元素值而不是位置
if (!book[i]) dfs(i);
book[cur] = true;
topo[temp--] = cur;
} int main()
{
while (~scanf("%d%d", &n, &m) && (n | m))//注意m是可以等于0的,n、m同时等于0才终止
{
//输入和预处理
for (int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
chu[a].push_back(b);//把b作为a的出度
}
//深搜
memset(book, false, sizeof(book));
temp = n;
for (int i = ; i <= n; i++)
if (!book[i]) dfs(i);
//输出
for (int i = ; i <= n; i++)
chu[i].clear(), printf("%d%c", topo[i], " \n"[i==n]);
}
}
UVA10305:Ordering Tasks(拓扑排序)的更多相关文章
- 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 (拓扑排序裸题)
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 (拓扑序列)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- UVA-10305 Ordering Tasks (拓扑排序)
题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...
- Uva10305 Ordering Tasks
John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...
随机推荐
- Long转换为date
public static void main(String[] args) { Long time = System.currentTimeMillis(); System.out.println( ...
- EnumDescription
using System; using System.Reflection; using System.Collections; using System.Collections.Generic; n ...
- Java中String args[]起什么作用?
在百度知道上看到这样一个答案: 在命令提示符中运行该程序时 可以附加参数运行 输入的参数会存入到字符传数组 args[]中例如:在命令提示符中运行该程序的时候假设该程序在D的JAVA文件夹中D:JAV ...
- BZOJ_1004_[HNOI2008]Cards_burnside+DP
BZOJ_1004_[HNOI2008]Cards_burnside+DP Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问 ...
- NOI2017退役记
Day1 全世界都200+我162,考场上fread和fwrite写挂了直接删了,然后就被卡了48也是没谁了. Day2 2-SAT写挂,就没有然后了. 明明退役前一直都在做自己最想做的事情,连这就是 ...
- alsa音频驱动框架
sound/core/sound.c 实现了最顶层的file_operations,它起中转作用 sound/core/control.c 实现了控制接口的file_operations sound/ ...
- 微信小程序内嵌网页能力开放 小程序支持内嵌网页文档说明
为了方便开发者灵活配置微信小程序,张小龙现在开放了小程序的内嵌网页功能,这是一个非常大的惊喜啊,以后意味着你只要开放一个手机端网站,就可以制作一个小程序了哦.操作方法1.开发者登录微信小程序后台,选择 ...
- Redis GEO 特性在 LBS 中的应用总结
什么是LBS LBS(Location Based Service),基于位置的服务. Redis和GEO Redis 是最热门的 nosql 数据库之一,它的最大特点就是快.所以在 LBS 这种需要 ...
- android开发中怎么通过Log函数输出当前行号和当前函数名
public class Debug { public static int line(Exception e) { StackTraceElement[] trace = e.getStackTra ...
- 设计模式学习笔记 1.factory 模式
Factory 模式 用户不关心工厂的具体类型,只知道这是一个工厂就行. 通过工厂的实现推迟到子类里面去来确定工厂的具体类型. 工厂的具体类型来确定生产的具体产品. 同时用户不关心这是一个什么样子的产 ...