Ordering Tasks

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
The input will consist of several instances of the problem. Each instance begins with a line containing
two integers, 1 ≤ n ≤ 100 and m. 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.
An instance with n = m = 0 will finish the input.
Output
For each instance, print a line with n integers representing the tasks in a possible order of execution.
Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3

约翰有n个任务要做, 不幸的是,这些任务并不是独立的,执行某个任务之前要先执行完其他相关联的任务

题解:拓扑排序模版题;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=110;
int que[MAXN];
int mp[MAXN][MAXN];
int n;
int vis[MAXN];
int ans[MAXN];
void topu(){
int k=0,a;
mem(vis,0);
while(k<n){
int temp=INF;
for(int i=1;i<=n;i++){
if(!vis[i]&&que[i]==0){
ans[k++]=i;temp=i;
break;
}
}
if(temp==INF)break;
vis[temp]=1;
for(int i=1;i<=n;i++)if(mp[i][temp]){
que[i]--; }
}
for(int i=0;i<k;i++){
if(i)P_;
PI(ans[i]);
}
puts("");
}
int main(){
int m,a,b;
while(scanf("%d%d",&n,&m),n|m){
mem(que,0);
mem(mp,0);
while(m--){
SI(a);SI(b);
que[b]++;
mp[b][a]=1;
}
topu();
}
return 0;
}

  dfs也可以写拓扑排序:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=110;
int mp[MAXN][MAXN];
int vis[MAXN],ans[MAXN];
int n;
int k;
bool dfs(int u){
vis[u]=-1;
for(int i=1;i<=n;i++){
if(mp[u][i]){
if(vis[i]==-1)return false;//表示结点v正在访问中,(即调用dfs(u)还在栈中,尚未返回)
if(!vis[i])dfs(i);
}
}
ans[--k]=u;
vis[u]=1;
return true;
}
bool topu(){
mem(vis,0);
k=n;
for(int i=1;i<=n;i++){
if(!vis[i])if(!dfs(i))
return false;
}
for(int i=0;i<n;i++){
if(i)P_;
PI(ans[i]);
}
puts("");
return true;
}
int main(){
int m,a,b;
while(scanf("%d%d",&n,&m),n|m){
mem(mp,0);
while(m--){
SI(a);SI(b);
mp[a][b]=1;
}
topu();
}
return 0;
}

  

Ordering Tasks(拓扑排序+dfs)的更多相关文章

  1. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

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

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

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

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

  4. Ordering Tasks 拓扑排序

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

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

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

  6. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  7. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  8. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  9. POJ1128 Frame Stacking(拓扑排序+dfs)题解

    Description Consider the following 5 picture frames placed on an 9 x 8 array.  ........ ........ ... ...

随机推荐

  1. codeforces 653D. Delivery Bears 网络流

    题目链接 我们二分每个人携带的数量, 然后每个边的容量就相当于min(权值/二分的值, x). x是人的数量. 然后判断是否满流就可以. 这么裸的网络流为竟然没看出来. 注意写fsbs(r-l)> ...

  2. hdu 2276 Kiki & Little Kiki 2 矩阵快速幂

    题目链接 n个灯围成一圈, 1左边是n. 有两种状态, 1是亮, 0是不亮. 如果一个灯, 它左边的灯是亮的, 那么下一时刻这个灯就要改变状态, 1变为0, 0变为1. 给出初始状态和时间t, 问t时 ...

  3. Sencha Touch 之 Ext.ComponentManager.get方法使用

    HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  4. SQL Server 执行计划重编译的两大情况

    1.与正确性相关的重编译 1.为表或视图添加列,删除列. 2.为表添加约束.默认值.规则,删除约束.默认值.规则. 3.为表或视图添加索引. 4.如果计划用不用索引而这个索引被删除. 5.删除表中的统 ...

  5. C#实现邮件发送功能

    发送邮件所用的核心知识点 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址.标题.主体.图片等等) 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配 ...

  6. Hibernate get 和load的区别

    1 load是要用的时候才从数据库去查询,get 是马上查询. 2 对于不存在的记录,get会报空指针异常,load会报 org.hibernate.ObjectNotFoundException:  ...

  7. Oracle Spatial-元数据及SDO_GEOMETRY

    一.空间表的元数据 将表的SDO_GEOMETRY列的所有对象作为一个空间层.Spatial需要对所有空间对象进行验证.创建索引和查询.此时需要为图层指定适当的元数据,该数据包含如下信息:维度.维度边 ...

  8. New Relic——手机应用app开发达人的福利立即就到啦!

    HiWork集成的第三方服务(机器人)将有新的添加啦,添加了BitBucket和New Relic.分别做下介绍啦! 1.BitBucket BitBucket 是一家源码托管站点.採用Mercuri ...

  9. Effective JavaScript Item 36 实例状态仅仅保存在实例对象上

    本系列作为EffectiveJavaScript的读书笔记. 一个类型的prototype和该类型的实例之间是"一对多"的关系.那么,须要确保实例相关的数据不会被错误地保存在pro ...

  10. Tomcat从零开始(十一)WebappLoader概述

    好的,我们先看看这个WebappLoader到底在开始的时候做了什么,先看看他的start()方法. public void start() throws LifecycleException { / ...