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的更多相关文章

  1. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  2. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  3. 拓扑排序(Topological Order)UVa10305 Ordering Tasks

    2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...

  4. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  5. M - Ordering Tasks(拓扑排序)

    M - Ordering Tasks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descri ...

  6. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

  7. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. UVA10305:Ordering Tasks(拓扑排序)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

随机推荐

  1. DateDiff函数

    在MySQL中可以使用DATEDIFF()函数计算两个日期之间的天数 语法: datediff(date1,date2) 注:date1和date2需是合法的日期或日期/时间表达式 例1 SELECT ...

  2. Excel 自定义函数

    浅谈Excel开发:四 Excel 自定义函数   我们知道,Excel中有很多内置的函数,比如求和,求平均,字符串操作函数,金融函数等等.在有些时候,结合业务要求,这些函数可能不能满足我们的需求,比 ...

  3. Entity Framework:如果允许模型处于非法状态,在某些场景下,记得清空DbContext

    Entity Framework:如果允许模型处于非法状态,在某些场景下,记得清空DbContext 背景 之前写过两篇文章介绍模型的合法性: DDD:关于模型的合法性,Entity.IsValid( ...

  4. Ubuntu 下的环境变量配置

    网上很多配置jdk环境变量的方法,但是几乎都会下次重启电脑就失效,或者时不时的失效.下面教你一招 JDK环境变量配置如下: 执行命令sudo gedit /etc/environment,在打开的编辑 ...

  5. Android开发(19)---常见dialog对话框的运用

    Dialog是android开发过程中最常用到的组件之一,Dialog的创立办法有两种: 一是直接new一个Dialog目标,然后调用Dialog目标的show和dismiss办法来操控对话框的显现和 ...

  6. 无U盘安装Linux openSUSE(通过硬盘安装Linux)

    一.说明 为什么会想着用硬盘安装Linux?只是因为我陆陆续续买了两个U盘,然后它们都丢了,就没再买了.然而现在又想装个openSUSE,没有U盘,只能想办法通过硬盘安装. 记录自己走过的弯路,同时也 ...

  7. Eclipse 中,web项目在Tomcat运行时填写不了Server name

    最近开发项目,从MyEclipse中导入项目到Eclipse中,那些WEB属性都在,可就是不能在Tomcat上运行.纠结一番,最后发现是这个问题: WEB的版本问题. 具体问题看下图: 解决方案: W ...

  8. jemeter逻辑控制器

    1.ForEach控制器 ForEach控制器在用户自定义变量中读取一系列相关的变量.该控制器下的采样器或控制器都会被执行一次或多次,每次读取不同的变量值.所以ForEach总是和User Defin ...

  9. java-并发解决方案

    并发产生数据不一致的原因:1.程序共享对象:2.多线程.3.基于1和2,取出来的数据可能不是最新的. 解决方案:只要是原子性操作,就不会出现问题.原子性操作,代表cpu会一直执行这个操作,知道结束. ...

  10. 第12章 MySQL高级管理

    1.手动更新权限后,需向服务器指出已对权限进行修改: (在MySQL提示符下)flush privileges; 2.查看用户所拥有的权限: 如: show grants for bookorama; ...