John有n个任务,但是有些任务需要在做完另外一些任务后才能做。

输入

输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 mn是任务个数(标记为1n),m两个任务直接关系的数量。在此之后,有m行,每行有2个整数ij,代表任务i必须在任务j之前完成。用n = m = 0结束整个输入。

输出

每一个数据对应一行n个整数,代表任务完成的顺序。

样例输入

	5 4
	1 2
	2 3
	1 3
	1 5
	0 0

样例输出

	1 4 2 5 3
分析:这就是一道模板题吧,用dfs版本处理比较好输出.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std;
const int maxn = ;
int n,ans[maxn],t,m,head[maxn],to[maxn],flag[maxn],nextt[maxn],tot = ; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void dfs(int u)
{
flag[u] = ;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!flag[v])
dfs(v);
}
ans[t--] = u;
} void solve()
{
memset(flag,,sizeof(flag));
t = n;
for (int i = ; i <= n; i++)
if (!flag[i])
dfs(i);
} int main()
{
while (scanf("%d%d",&n,&m) == && (n || m))
{
tot = ;
memset(head,,sizeof(head));
for (int i = ; i <= m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
solve();
for (int i = ; i <= n; i++)
{
if (i != n)
printf("%d ",ans[i]);
else
printf("%d",ans[i]);
}
printf("\n");
} return ;
}
 

Uva10305 Ordering Tasks的更多相关文章

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

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

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

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

  3. UVA10305 Ordering Tasks (拓扑序列)

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

  4. Ordering Tasks UVA - 10305 图的拓扑排序

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

  5. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  6. [SOJ] Ordering Tasks

    1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n task ...

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

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

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

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

  9. UVA10305:Ordering Tasks(拓扑排序)

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

随机推荐

  1. robotframework - 测试用例&套件- Settings标签

    1.Test Case -- Settings标签截图 2.Test Case Settings 标签说明: Documentation:用于描述用例的一个小文本,它可以把 URL 地址转换为可点击的 ...

  2. centos 7 配置php

    对于我们的目的而言,安装 Apache 只需要在 CentOS 命令终端敲入这条命令就行了: $ sudo yum install httpd $ sudo systemctl enable http ...

  3. java IO流 之 字节输出流 OutputString()

    Java学习重点之一:OutputStream 字节输出流的使用 FileOutPutStream:子类,写出数据的通道 步骤: 1.获取目标文件 2.创建通道(如果原来没有目标文件,则会自动创建一个 ...

  4. Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决

    问题描述 Android Studio 1.5是当前最新Android手机应用开发平台,下载bundle版安装后,启动Studio后出现“SDK Manager: failed to install” ...

  5. Eclipse 编译java文件后出错 左树无红叉

    问题描述: 今天遇见让人郁闷的问题,在项目工程中java文件编译通不过,eclipse在java文件中标示错误,但是却不不能在navigator的视图中像平常一样出现小红叉.通过clean proje ...

  6. JPQL 模糊查询,查询条件拼接(like使用)

    @Transactional public List<ViewCorplist2> findAllCorpsLikeK(String kw) { System.out.println(kw ...

  7. 没搞错吧,我只是个web前端工程师,不是manager,也不是leader...

    那个时候,我只想好好的学习web前端技术,恨不得把有限的时间和精力都放在提升技术上. 然而,让自己在坑里茁壮成长,要先适应坑内的环境. 首当其冲我们要弄明白的事情有: 团队成员的技术能力和状态 Lea ...

  8. [iOS]查看苹果支持的所有字库

    有时候,在开发的时候,想换个字体更加优雅的显示.但是不知道苹果本身到底支持了那些字体,怎么办? 没关系,打开这个网址:http://iosfonts.com/ 所见即所得,非常方便,而且还表明了从那个 ...

  9. 梦想MxWeb3D,三维CAD协同设计平台 2019.05.05更新

    SDK开发包下载地址: http://www.mxdraw.com/ndetail_20140.html 在线演示网址: http://www.mxdraw.com:3000/ 1.  增加CAD绘图 ...

  10. JavaScipt30(第四个案例)(主要知识点:数组原型链上的一些方法)

    承接上文,下面是第四个案例 附上项目链接: https://github.com/wesbos/JavaScript30 const inventors = [ { first: 'Albert', ...