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 1637: [Usaco2007 Mar]Balanced Lineup【瞎搞】
我是怎么想出来的-- 把种族为0的都变成-1,按位置x排升序之后,s[i]表示种族前缀和,想要取(l,r)的话就要\( s[r]-s[l-1]==0 s[r]==s[l-1] \),用一个map存每个 ...
- 【题解】自行车比赛 [AHOI2016] [P2777]
[题解]自行车比赛 \([AHOI2016]\) \([P2777]\) 逼自己每天一道模拟题 传送门:自行车比赛 \([AHOI2016]\) \([P2777]\) [题目描述] 比赛中一共有 \ ...
- 对mysql修改库里面所有的引擎mysaim 为 innodb
1.查看当前数据引擎的命令: show engines; 2. USE `[DBNAME]`; SELECT GROUP_CONCAT(CONCAT( 'ALTER TABLE ' ,TABLE_NA ...
- CircuitBreaker design pattern---reference
It's common for software systems to make remote calls to software running in different processes, pr ...
- [ SDOI 2006 ] 保安站岗
\(\\\) Description 给出一棵 \(n\) 个节点以 \(1\) 为根的树,一个节点的覆盖半径是 \(1\) ,点有点权 \(val_x\) . 选择一些点,使得点权和最小,同时每个节 ...
- [ CodeForces 1059 C ] Sequence Transformation
\(\\\) \(Description\) 你现在有大小为\(N\)的一个数集,数字分别为 \(1,2,3,...N\) ,进行\(N\)轮一下操作: 输出当前数集内所有数的\(GCD\) 从数集中 ...
- LR接口测试---socket
前提条件: 编译:javac TcpServer.java 启动:java TcpServer ============================================ 代码示例: # ...
- rem布局进入页面样式错乱解决
开发项目时候第一次遇到rem布局进入页面瞬间样式错乱问题: //该段js为rem布局应用 如10px = 0.1rem; (function(doc, win) { var docEl = doc.d ...
- 04C语言输入输出
C语言输入输出 输入字符getchar() #include <stdio.h> int main(){ putchar(getchar()); putchar(getchar()); ; ...
- css--小白入门篇1
一.引入 css用来描述html,学习css前我们先来学习html的基础标签的用法,再进入css的学习. 本教程面向小白对象,不会讲细枝末节深入的东西. 二.列表 列表有3种 2.1 无序列表 无序列 ...