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 ...
随机推荐
- P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- Java多线程(一) Thread和 Runnable
http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 1.继承Thread 2.实现Runnable接口 public class MyRunnable ...
- python自动化学习笔记10-数据驱动DDT与yml的应用
在测试工作中,针对某一API接口,或者某一个用户界面的输入框,需要设计大量相关的用例,每一个用例包含实际输入的各种可能的数据.通常的做法是,将测试数据存放到一个数据文件里,然后从数据文件读取,在脚本中 ...
- JS数组、outerHtml、className
//对象转换为数组function obj(){for(var i=0;i<arguments.length;i++){ this[i]=arguments[i]; }} var o2=new ...
- 常用JavaScript代码库(又名:WFang.js)
1.根据公司项目封装ajax请求,结合layer框架一起使用 /*提取接口公共部分*/ var ApiConf = { server:"http://localhost:8080/Batte ...
- Spring Boot (31) 数据验证
曾经参数的验证是这样的: public String test(User user){ if(user == null){ throw new NullPointerException("u ...
- Android 新闻app的顶部导航栏,怎么实现动态加载?
TabLayout + viewpager 其中viewpager的适配器要继承FragmentPagerAdapter,要实现动态更新,最主要的是适配器的写法,要在数据发生变化之后清除Fragmen ...
- Python批量生成用户名
写在最前 平时在工作中尤其是在做压测的时候难免需要一些用户名和密码,写个简单的Python小脚本批量生成一些 代码示例 import random,string #生成大小字母和数字一起的大字符串 a ...
- Concurrent control in SQLite
This document describes the technologies to concurrent access to a SQLite database. There are also s ...
- Caffe RPN:把RPN网络layer添加到caffe基础结构中
在测试MIT Scene Parsing Benchmark (SceneParse150)使用FCN网络时候,遇到Caffe错误. 遇到错误:不可识别的网络层crop 网络层 CreatorRegi ...