UVA-10305
Ordering Tasks
Time Limit: 3000MS |
Memory Limit: Unknown |
64bit IO Format: %lld &%llu |
Submit Status
Description
Problem F
Ordering Tasks
input: standard input
output: standard output
Time Limit: 1 second
Memory Limit: MB
John had n tasks to do. Unfortunately, the tasks independent and the execution of one task is only possible if and tasks with already be En executed.
Input
The input would consist of several instances of the problem. Each instance begins with a line containing the integers, 1 <= n <= and M. n is the number of the tasks (numbered from 1 to n) and m are the number of direct Preceden Ce relations between tasks. After this, there'll be m lines with II integers i and J, representing the fact that task i must be executed before task J. An instance with n = m = 0 would 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)
Source
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Graph:: Grap H Traversal:: Topological Sort
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Graph:: Graph Traversal:: Topological Sort
Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 6. Data Structures:: Examples
Root:: AOAPC i:beginning algorithm Contests (Rujia Liu):: Volume 2. Data Structures:: Graphs
Root:: Competitive programming:increasing The Lower Bound of programming contests (Steven & Felix Halim):: Chapter 4. Graph:: Depth First Search:: Topological Sort
Sort the topology on the petition!
AC Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int flag, N, M;int topo[ (num[110], s[110][110];bool dfs (int site) { num[site]=-1; for (int i=1; i<=n; i++) if (s[site][i]&& (num[i]<0| | (!num[i]&&!dfs (i)))) return false; Num[site]=1; Topo[--flag]=site; return true;} BOOL Toposort () { flag=n; memset (num,0,sizeof (num)); for (int i=1; i<=n; i++) if (!num[i]&&!dfs (i)) return false; return true;} int main () { int x,y,count; while (scanf ("%d%d", &n,&m)!=eof) { if (!n&&!m) break; Memset (s,0,sizeof (s)); for (count=0; count<m; count++) { scanf ("%d%d", &x,&y); s[x][y]=1; } if (Toposort ()) for (count=0; count<n-1; count++) printf ("%d", Topo[count]); printf ("%d\n", Topo[count]); } return 0;}
Uva-10305-ordering Tasks (topological sort!) )