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. 纯CSS 贴底部的布局(兼容各个浏览器包括IE6)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. Android中Dialog对话框

    布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  3. Facebook 调试工具Stetho配置入门

    I decided to spend a few hours on Stetho.Stetho is a sophisticated debug bridge for Android applicat ...

  4. KETTLE使用入门

    一.准备文件 1.安装java虚拟机 2.安装kettle安装文件 二.使用步骤 1.点击Spoon.bat,启动kettle,弹出DOS窗口如下: 2.进入主界面 3.新建资源库

  5. Repeat Number

    Problem B: Repeat Number Time Limit: 1 Sec  Memory Limit: 32 MB Description Definition: a+b = c, if ...

  6. QT中进度条的使用

    在QT中可以用QProgressBar或着QProgressDialog来实现进度条. QProgressBar的使用 首先在designer中拖一个按钮和进度条部件,按下面初始化 //补充:下面两句 ...

  7. 汇编写下strcpy

    #include <stdio.h> int main() { char *source = "hello world\n"; ] = {}; char *p = de ...

  8. 转: ES6异步编程:Thunk函数的含义与用法

    转: ES6异步编程:Thunk函数的含义与用法 参数的求值策略 Thunk函数早在上个世纪60年代就诞生了. 那时,编程语言刚刚起步,计算机学家还在研究,编译器怎么写比较好.一个争论的焦点是&quo ...

  9. 解密电子书之四:MCU(freescale)

    谈完国产的君正,让我们再看看呛了君正财路的freescale iMX51. 这是freescale近期的主打产品,用的是ARM Cortex A8架构,主频在消费电子领域最高可达800MHz,在工业领 ...

  10. LDA的一些资料

    LDA-math-汇总 LDA数学八卦 http://www.52nlp.cn/lda-math-%E6%B1%87%E6%80%BB-lda%E6%95%B0%E5%AD%A6%E5%85%AB%E ...