Ordering Tasks
来源:互联网 发布:移动教学软件 编辑:程序博客网 时间:2024/05/22 06:08
Description
Problem F
Ordering Tasks
Input: standard input
Output: standard output
Time Limit: 1 second
Memory Limit: 32 MB
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
(The Joint Effort Contest, Problem setter: Rodrigo Malta Schmidt)
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int MAP[110][110],c[110],n,m,t;int topo[110];bool dfs(int u){ c[u]=-1; for(int v=1; v<=n; v++) if(MAP[u][v]) { if(c[v]< 0) return false; else if(c[v]==0&&!dfs(v)) return false; } c[u]=1; topo[t--]=u; return true;}bool toposort(){ t=n; memset(c,0,sizeof(c)); for(int u=1; u<=n; u++) if(!c[u]) if(!dfs(u)) return false; return true;}int main(){ int a,b; while(scanf("%d%d",&n,&m)!=EOF&&(n+m)!=0) { memset(MAP,0,sizeof(MAP)); for(int i=1; i<=m; i++) { cin>>a>>b; MAP[a][b]=1; } if(toposort()) { printf("%d",topo[1]) ; for(int i=2; i<=n; i++) printf(" %d",topo[i]); cout<<endl; } } return 0;}
- Ordering Tasks
- Ordering Tasks
- Ordering Tasks
- Ordering Tasks
- uva 10305 - Ordering Tasks
- Sicily 1940. Ordering Tasks
- uva 10305 - Ordering Tasks
- UVaOJ 10305 - Ordering Tasks
- uva 10305 - Ordering Tasks
- UVa 10305 - Ordering Tasks
- uva-10305 - Ordering Tasks
- UVa 10305 - Ordering Tasks
- 10305 - Ordering Tasks
- UVa 10305 - Ordering Tasks
- UVA10305- Ordering Tasks
- UVaOJ10305---Ordering Tasks
- UVA 10305 Ordering Tasks
- UVa 10305Ordering Tasks
- 怎么实现Web聊天
- 数据结构之并查集
- dhtmlxgrid数据表格在ie9,ie10下无法隐藏列
- 关于Oracle分页SQL
- Balancer - CodeForces 440B
- Ordering Tasks
- C++以阻塞的方式调用外部exe程序,等待其运行结束
- C开源hash代码uthash的用法总结(1)
- 百度有道雅虎的实习面试经历
- URL的组成
- 如何注册一个新的form到EBS系统
- ArcGIS教程:河网分级的工作原理
- 北邮新生排位赛2解题报告a-c
- 【solr专题之四】在Tomcat 中部署Solr4.x