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. javaTemplates-学习笔记三

    Routes入口 后台语言的应用入口都是从routes开始的,想要新建一个页面得学会配置routes. conf/routes 文件定义了全部应用URL的动作(Action),如果当浏览器请求访问ht ...

  2. mac使用小技

    xcodeブラックスクリーンの解決策: 1.cd ~/Library/Developer/Xcode/DerivedData 2.rm -fr *    //注释:-fr和*是分开的3.关闭模拟器,关 ...

  3. 图的邻接矩阵实现(c)

    参考:算法:c语言实现 一书 图的邻接矩阵实现 #ifndef GRAPH #define GRAPH /* 图的邻接矩阵实现 */ #include<stdio.h> #include& ...

  4. property、synthesize、id

    1.@property int age; 在编译器情况下会自动编译展开为: <age在setter中首字母大写,点语法为p.age> - (void)setAge:(int)age; - ...

  5. 9.java.lang.ClassCastException

    java.lang.ClassCastException 数据类型转换异常 当试图将对某个对象强制执行向下转型,但该对象又不可转换又不可转换为其子类的实例时将引发该异常,如下列代码. Object o ...

  6. Oracle EBS-SQL (SYS-2): sys_在线用户查询.sql

    SELECT fs.USER_NAME,       fu.description,       fs.RESPONSIBILITY_NAME,       fs.USER_FORM_NAME,    ...

  7. SpringMVC 中整合之JSON、XML

    每次看到好的博客我就想好好的整理起来,便于后面自己复习,同时也共享给网络上的伙伴们! 博客地址: springMVC整合Jaxb2.xStream:  http://www.cnblogs.com/h ...

  8. delphi 编码速度提升技能

    效率,是一个永恒的主题. 本文重点强调 delphi ide 中的编码速度技能 一.TForm 窗口重用 当您在写一个管理类软件的时候,有大量的操作窗口,这些窗口会有大量共性.窗口重用就会发挥很大的作 ...

  9. nodejs开发微信1——微信access-token和tickets的数据模型

    /* jshint -W079 */ /* jshint -W020 */ "use strict"; //var _ = require("lodash"); ...

  10. oracle添加用户及权限

    CREATE USER qdcenter 用户名 IDENTIFIED BY qdcenter   密码 DEFAULT TABLESPACE data1  默认表空间 TEMPORARY TABLE ...