[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 ...
随机推荐
- .Net 异步随手记(三)
从<.Net 异步随手记(二)>来看,总感觉还差点儿什么,就是对不同情况的处理.比如当一个 Task 完成了后,我想让它继续执行 T1,如果被取消了就去执行 T2,如果...就去执行 T3 ...
- 跨平台C/C++集成开发环境-Code::Blocks-内置GCC
Code::Blocks 是一个开放源码的全功能的跨平台C/C++集成开发环境. 相比于基于Delphi的Dev-C++共享C++IDE,Code::Blocks是开放源码软件.Code::Block ...
- Coding the Matrix作业Python Lab及提交方法
Coding the Matrix: Linear Algebra through Computer Science Applications 这是一门用python实现矩阵运算的课,第一次作业就感觉 ...
- QTime中currentTime()的使用方法
QTime中currentTime()的使用方法 QTime是Qt中一个处理时间的类,其成员函数currentTime()是最经常用到函数,下面举例说明该成员函数如何使用. 1. 包含头文件 #inc ...
- 分享一个javascript alert精简框架
如果你不喜欢浏览器自带的alert你可以尝试总共不超过10KB somke js 下载地址:http://smoke-js.com/ 使用方法 somke.alert("hello wor ...
- mysql处理存在则更新,不存在则插入(多列唯一索引)
mysql处理某个唯一索引时存在则更新,不存在则插入的情况应该是很常见的,网上也有很多类似的文章,我今天就讲讲当这个唯一的索引是多列唯一索引时可能会遇到的问题和方法. 方法一: 使用 INSERT I ...
- UIColor RGB颜色对照表
色 彩 RGB 值 对 照 表 color red green blue Hexadecimal triplet example Aliceblue 240 248 255 f0f8ff ...
- json文件报expected name at 1 1错误
在eclipse编辑json文件时出现expected name at 1 1错误,解决方式如下: 方法一:json文件是不支持注释的,把所有/* content */删除即可 如果不想删除注释-- ...
- 查找附近的wifi密码
无意之间看到能破解附近wifi密码的诀窍,赶紧存储下来. 1. 首先打开终端 2. 在没有网路的情况下输入: netsh wlan show profiles 结果如下(自己的): 这些'用户配置文件 ...
- C#_单例模式
单例:在程序的整个进程中只会被实例化一次 如:User user =new User();实例化一个User();的时候new User()是调用的 User类的 默认的公有构造函数:public U ...