Uva10305 Ordering Tasks
John有n个任务,但是有些任务需要在做完另外一些任务后才能做。
输入
输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m。n是任务个数(标记为1到n),m两个任务直接关系的数量。在此之后,有m行,每行有2个整数i和j,代表任务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的更多相关文章
- 拓扑排序(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 (拓扑序列)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- [SOJ] Ordering Tasks
1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n task ...
- 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 ...
- UVA10305:Ordering Tasks(拓扑排序)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
随机推荐
- bzoj 1598: [Usaco2008 Mar]牛跑步【A*K短路】
A*K短路模板,详见https://blog.csdn.net/z_mendez/article/details/47057461 算法流程: 把有向图全建成反向边,跑一遍所有点到t的最短路记为dis ...
- P4128 [SHOI2006]有色图
传送门 数学渣渣看题解看得想死Ծ‸Ծ 首先发现这玩意儿看着很像polya定理 \[L=\frac{1}{|G|}\sum_{i\in G}m^{w(i)}\] 然而polya定理只能用来求点的置换,边 ...
- Syntax error on token ";", , expected 错误
eclipse错误提示如图: 错误代码如图: 一开始百思不得其解,后来终于发现问题的原因所在,java中变量的声明可以不在方法中,但语句只能出现在方法中,可以再声明变量的时候就赋初值,但如果要单独赋值 ...
- 大数高精度加减乘除 51nod 1005 大数加法
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...
- 转 awr自动收集脚本
1. remote get awr report #!/usr/bin/ksh ####sample: sh awr.sh 20170515 20170516 AWR ### default it w ...
- CircuitBreaker design pattern---reference
It's common for software systems to make remote calls to software running in different processes, pr ...
- 关于.Net中Process的使用方法和各种用途汇总(一):Process用法简介
简介: .Net中Process类功能十分强大.它可以接受程序路径启动程序,接受文件路径使用默认程序打开文件,接受超链接自动使用默认浏览器打开链接,或者打开指定文件夹等等功能. 想要使用Process ...
- ReLU激活函数:简单之美
出自 http://blog.csdn.net/cherrylvlei/article/details/53149381 导语 在深度神经网络中,通常使用一种叫修正线性单元(Rectified lin ...
- Selenium基于Python web自动化测试框架 -- PO
关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...
- apache hadoop 伪分布式安装
1. 准备工作 1.1. 软件准备 1.安装VMWare 2.在VMWare上安装CentOS6.5 3.安装XShell5,用来远程登录系统 4.通过rpm -qa | grep ssh 检查cen ...