Ordering Tasks(拓扑排序+dfs)
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)的更多相关文章
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- M - Ordering Tasks(拓扑排序)
M - Ordering Tasks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- 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 ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
随机推荐
- Flink资料(2)-- 数据流容错机制
数据流容错机制 该文档翻译自Data Streaming Fault Tolerance,文档描述flink在流式数据流图上的容错机制. ------------------------------- ...
- larbin源码之global.h
/** This represent a connection : we have a fixed number of them * fetchOpen links them with servers ...
- 新手笔记-tftp与yum
lspci 查看pci插槽设备 lsusb 查看USB设备 tftp 配置文件 /etc/xinetd.d/tftp tftp 根目录 /var/lib/tftpboot service xinet ...
- windows环境中mysql忘记root密码的解决办法
原文地址:http://www.cnblogs.com/linuxnotes/archive/2013/03/09/2951101.html windows下重置Mysql Root密码的方法mysq ...
- C++死锁解决心得
一. 概述C++多线程开发中,容易出现死锁导致程序挂起的现象.关于死锁的信息,见百度百科http://baike.baidu.com/view/121723.htm. 解决步骤分为三步:1.检测死锁线 ...
- DKNY_百度百科
DKNY_百度百科 DKNY
- Hibernate 数据的批量插入、更新和删除
4.2 Hibernate的批量处理 Hibernate完全以面向对象的方式来操作数据库,当程序里以面向对象的方式操作持久化对象时,将被自动转换为对数据库的操作.例如调用Session的delete ...
- 截取字符串一之slice
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java中文乱码问题研究(二)
上面写了console的乱码问题,接下来写的是web中servlet中的问题,大楷我比较关心一点,因为遇到这个的情况多一些吧.直接开始吧. 2. jsp和servlet中的乱码问题 分析: a. 其实 ...
- HTML之学习笔记(六)添加链接
html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签 <a href="URL">热点文字 ...