UVA - 10305 - Ordering Tasks (拓扑排序!)

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 mn 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\n",topo[count]);
    }
    return 0;
}
时间: 2024-10-17 07:54:04

UVA - 10305 - Ordering Tasks (拓扑排序!)的相关文章

UVa 10305 - Ordering Tasks 拓扑排序题解

Topological Sort题解.本题是简单的入门题目. Topological Sort的思想很简单,就是按没有入度的点,先输出,然后删除这个点的出度.然后输出下一组没有入度的点. 如何实现也是很简单的: 这里使用邻接表,建图的时候反过来建图,建立一个入度邻接表. 然后使用一个vis数组,记录访问过的节点,也可以根据这个信息知道哪些是已经输出的点,这个时候这些点的入度可以不算为当前入度了. #include <stdio.h> #include <vector> using

UVa 10305 - Ordering Tasks (拓扑排序裸题)

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

uva 10305 Ordering Tasks(拓扑排序)

拓扑排序,不用判断是否有环,dfs挺简单的 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int map[105][105]; int visit[105]; int c[105]; int n,m,t; void dfs(int x) { visit[x] = 1; for(int i=1; i<=n; i++) { if(!visit[i]&&map[i][x]==1)

uva 10305 Ordering Tasks (简单拓扑)

uva 10305 Ordering Tasks 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.

UVA 10305 Ordering Tasks (拓扑排序)

题意:给你n个点.m个关系,每个关系两个点u.v,表示u小于v,叫你输出任意一个序列保证满足所有给定的关系 例如:n=3 m=2 1 2 3 1 3 2 3 1 2 题解:拓扑排序排的是一个有向无环图(DAG),首先没有回路,否则会失败,其次如果存在G(u,v),则在该序列中u在v前面 实现方法就是遍历每个点,当此点没被标记过就进入递归 然后将此点看做树的根节点(DAG其实可以看做森林),遍历它可以到的所有点,最后从后向前将点加入排序的序列并标记 #include<cstdio> #inclu

UVa 10305 - Ordering Tasks【拓扑排序】

Ordering Tasks 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 insta

UVA - 10305 Ordering Tasks(拓扑排序)

题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #i

Uva 10305 Ordering Tasks(拓扑排序模版题)

Uva 10305 #include<iostream> #include<queue> #define pb push_back #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double pi=acos(-1.0); const int N=1e2+5; using namespace std; int n,m; int du[N]; vector<int>

[2016-02-17][UVA][10305][Ordering Tasks]

时间:2016-02-17 20:18:58 星期三 题目编号:UVA 10305 题目大意:给定n个任务,和m行信息,每行信息包括 i j,表示第i个任务,必须在第j个任务之前完成,输出,任意一个,任务完成的顺序 分析:相当于,给定若个a,b的大小关系,求所有数字可能的大小关系, 方法:拓扑排序   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36