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(拓扑排序)的更多相关文章

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

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

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

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

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

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

  4. Ordering Tasks 拓扑排序

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

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

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

  6. UVA10305 Ordering Tasks (拓扑序列)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...

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

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

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

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

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

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

  10. Uva10305 Ordering Tasks

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

随机推荐

  1. sort quick

    package com.demo; import java.util.ArrayList; import java.util.List; public class SearchSort { publi ...

  2. IOC入门1

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  3. druid.io 海量实时OLAP数据仓库 (翻译+总结) (1)——分析框架如hive或者redshift(MPPDB)、ES等

    介绍 我是NDPmedia公司的大数据OLAP的资深高级工程师, 专注于OLAP领域, 现将一个成熟的可靠的高性能的海量实时OLAP数据仓库介绍给大家: druid.io NDPmedia在2014年 ...

  4. PS 图像滤镜— — USM 锐化

    这个算法的原理很简单,就是先用高斯模糊获取图像的低频信息,然后用原图减去高斯模糊之后的图,得到图像的高频信息,再将原图与高频信息融合,进一步增强原图的高频信息,看起来,图像的边缘显得特别的sharp. ...

  5. Numpy 小结

    Python 真火来学习一下,先来看一个库 NumPy.NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. 1. 读取文件 num ...

  6. java并发之阻塞队列LinkedBlockingQueue与ArrayBlockingQueue

    Java中阻塞队列接口BlockingQueue继承自Queue接口,并提供put.take阻塞方法.两个主要的阻塞类实现是ArrayBlockingQueue和LinkedBlockingQueue ...

  7. jquery data 选择器 表格序列化serialize()

    data()在元素上存放或者读取数据,返回jquery对象. demo: <div data-obj="{'name':'zhangsan','age':20}">&l ...

  8. [置顶] 谈EXPORT_SYMBOL使用

    转自:http://blog.csdn.net/macrossdzh/article/details/4601648 EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static ...

  9. lightoj 1027【数学概率】

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e2+10; int ma ...

  10. OPENGL4_变换

    几种变换的几何意义说明 http://blog.csdn.net/onafioo/article/details/22094247 变换的执行顺序问题 正常顺序 1 视图(观察)变换 2 模型变换 3 ...