uva 10305 - Ordering Tasks
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
, 0 , 。
#include<stdio.h>
int n,step,a[101][101],visit[101];
int sort()
{int i,x=0,y,f=1;
if (step<=0) return 0;
--step;
while (f)
{++x; y=0;
if (visit[x])
{ for (i=1;i<=n;i++)
{y=y+a[i][x];
if (y>0) break;
}
f=y;
}
}
for (i=1;i<=n;i++)
a[x][i]=0;
visit[x]=0;
printf("%d",x);
if (step) printf(" ");
else printf("
");
sort();
return 0;
}
int main()
{int x,y,i,j,m;
while (scanf("%d%d",&n,&m),n+m)
{for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
a[i][j]=0;
for (i=1;i<=n;i++)
visit[i]=1;
for (i=1;i<=m;i++)
{scanf("%d%d",&x,&y);
a[x][y]=1;
}
step=n;
sort();
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
application ---- enter and save the file codeInput file name and content - input_content.html Receive content and save file and content - input_content01.jsp...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.