[SOJ] Ordering Tasks
1940. Ordering Tasks
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
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.
Input
There are multiple test cases. The first line contains an integer T, indicating the number of test cases. Each test case begins with a line containing two integers, 1 <= n <= 100000 and 1 <= m <= 100000. 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. It is guaranteed that no task needs to be executed before itself either directly or indirectly.
Output
For each test case, print a line with n integers representing the tasks in a possible order of execution. To separate them, print exactly one space after each integer. If there are multiple solutions, output the smallest one by lexical order.
Sample Input
1
5 5
3 4
4 1
3 2
2 4
5 3
Sample Output
5 3 2 4 1
//AOV拓扑排序
#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std; int main()
{
int numTestcases;
cin >> numTestcases; while(numTestcases--)
{
int n, m;
cin >> n >> m;
int inDegree[n + 1]; //入度为0数组
int result[n]; //结果序列
vector<int> tasks[n + 1]; //每一组vector都有该结点的后继结点
memset(inDegree, 0, sizeof(inDegree)); //初始化 for (int i = 0; i < m; ++i)
{
int a, b;
cin >> a >> b;
inDegree[b]++;
tasks[a].push_back(b);
} priority_queue<int, vector<int>, greater<int> > readyTasks;//使用最小优先队列可以自动按照从小到大排序 for (int i = 1; i <= n; ++i)
{//先将所有根结点放进队列中待选
if(inDegree[i] == 0)
readyTasks.push(i);
} int numFinished = 0; while(!readyTasks.empty())
{
int cur = readyTasks.top();
result[numFinished++] = cur;
readyTasks.pop();
vector<int>::iterator it; for(it = tasks[cur].begin(); it != tasks[cur].end();it++)
{
int temp = *it;
inDegree[temp]--;
if(inDegree[temp] == 0)//当前趋结点全部完成时可以开始这个任务
readyTasks.push(temp);
}
} for (int i = 0; i < n; ++i)
{
cout << result[i] << " ";
}
cout << endl;
}
return 0;
}
[SOJ] Ordering Tasks的更多相关文章
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- M - Ordering Tasks(拓扑排序)
M - Ordering Tasks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Ordering Tasks 拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA10305:Ordering Tasks(拓扑排序)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
随机推荐
- 如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作
如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作 其实这个问题已经在<SQLSERVER企业级平台管理实践>里徐老师已经讲过了,不过我想用自己的语言再讲详细一些 笔记链接 ...
- 数组和Hash表
数组和Hash表 当显示多条结果时,存储在变量中非常智能,变量类型会自动转换为一个数组. 在下面的例子中,使用GetType()可以看到$a变量已经不是我们常见的string或int类型,而是Obje ...
- jQuery获取checkbox选中项等操作及注意事项
jQuery获取checkbox选中项等操作及注意事项 今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下 ...
- 强烈推荐:240多个jQuery插件【转】
强烈推荐:240多个jQuery插件 概述 jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只 ...
- 三种不同实现初始化和销毁bean之前进行的操作的比较
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...
- yowsup ( an application to use whatsapp) hack
yowsup, in python https://github.com/tgalal/yowsup try this: http://hacktracking.blogspot.com.ar/201 ...
- 不再害羞,过程比结果更重要;分享一套 CodeSmit 代码生成模板。
住博客园 5 年了,以前也发过一些博文,但都在 一天后 / 几周后 / 几年后 将它删了:因为感觉代码写得不好:不清晰或侵入太大,哪怕只有一句侵入. 可是最近重写一套 CodeSmith 代码生成模板 ...
- Android应用中使用AsyncHttpClient来异步网络数据
首先下载AsyncHttpClient的库文件,可以自行搜索,可以到下面地址下载 http://download.csdn.net/detail/xujinyang1234/5767419 测试的Ac ...
- OGG学习笔记01-基础概述
OGG学习笔记01-基础概述 OGG(Oracle Golden Gate),最近几年在数据同步.容灾领域特别火,甚至比Oracle自己的原生产品DataGuard还要风光,主要是因为其跨平台.跨数据 ...
- putty连接远程局域网的MySql(不需要单独打开plink)
3316 是本地端口,映射到远程内网的一台MySql主机 10.8.2.172