码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-19 00:17:26      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

[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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxn = 100 + 10;
int n,m,g[maxn][maxn],vis[maxn],topo[maxn],t;
void dfs(int u){
        vis[u] = 1;
        FOR(v,1,n+1){
                if(g[u][v] && !vis[v]){
                        dfs(v);
                }
        }
        topo[--t] = u;
}
void toposort(){
        t = n;
        CLR(vis,0);
        FOR(u,1,n+1)    if(!vis[u])     
                dfs(u);
}
int main(){
        while(~scanf("%d%d",&n,&m) && (n || m)){
                int a,b;
                CLR(g,0);
                FOR(i,0,m){
                        scanf("%d%d",&a,&b);
                        g[a][b] = 1;
                }
                toposort();
                printf("%d",topo[0]);
                FOR(i,1,n){
                        printf(" %d",topo[i]);
                }
                puts("");
        }
    return 0;
}








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

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/5199594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!