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: 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)
Source
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Graph 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
백서 의 토폴로지 정렬!
AC 코드:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int flag, n, m;
int topo[110], 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
",topo[count]);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.