M - Ordering Tasks(拓扑排序)

M - Ordering Tasks

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

 

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



//这题的意思是这样的,第一行输入n,m,两个整数,说明有 1-n 个数,m个要求,接下来m行每行一个要求,a b,a必须放在b前面,输出一种可行的方案
mn都为0结束输入。

这个题目其实就是拓扑排序,思路是将有要求的用一个二维数组存起来,和图一样,读完数后,从1开始到n,从有关联的并且未放置过的数一直dfs遍历,找到最后一个,也就是找到没有要放在这个数后面的了,将它们这一串放在没放过的数组后面。

DFS:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 
 6 int n,m;
 7 bool G[105][105];
 8 int topo[105];
 9 int vis[105];
10 int t;
11 
12 bool dfs(int u)
13 {
14     vis[u]=-1;                   //标记为正在遍历的
15     for (int v=1;v<=n;v++)
16     {
17         if (G[u][v])
18         {
19             if (vis[v]==-1) return 0;       //说明组成了环,不能拓扑排序
20             else if (!vis[v]&&!dfs(v)) return 0; //继续遍历未遍历的
21         }
22     }
23     vis[u]=1;       //标记遍历过了
24     topo[t--]=u;    //输出的就是这个数组
25     return 1;
26 }
27 
28 bool toposort()
29 {
30     t=n;
31     int i;
32     for (i=1;i<=n;i++)               //将所有数都遍历
33         if (!vis[i]&&!dfs(i)) return 0;
34     return 1;
35 }
36 int main()
37 {
38     int i;
39     int a,b;
40     while (scanf("%d%d",&n,&m)&&n+m)
41     {
42         memset(G,0,sizeof (G));
43         for (i=0;i<m;i++)
44         {
45             scanf("%d%d",&a,&b);
46             G [a][b]=1;          //记录关系
47         }
48         memset(vis,0,sizeof(vis));
49         if (toposort())
50         {
51              for (i=1;i<n;i++)
52              printf("%d ",topo[i]);
53              printf("%d\n",topo[n]);
54         }
55     }
56     return 0;
57 }
View Code

 Khan

 1 # include <cstring>
 2 # include <cstdio>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <vector>
 6 # include <queue>
 7 # include <stack>
 8 # include <map>
 9 # include <bitset>
10 # include <sstream>
11 # include <set>
12 # include <cmath>
13 # include <algorithm>
14 # pragma  comment(linker,"/STACK:102400000,102400000")
15 using namespace std;
16 # define LL          long long
17 # define pr          pair
18 # define mkp         make_pair
19 # define lowbit(x)   ((x)&(-x))
20 # define PI          acos(-1.0)
21 # define INF         0x3f3f3f3f3f3f3f3f
22 # define eps         1e-8
23 # define MOD         1000000007
24 
25 inline int scan() {
26     int x=0,f=1; char ch=getchar();
27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
29     return x*f;
30 }
31 inline void Out(int a) {
32     if(a<0) {putchar('-'); a=-a;}
33     if(a>=10) Out(a/10);
34     putchar(a%10+'0');
35 }
36 const int N = 105;
37 /**************************/
38 
39 int n;
40 int mp[N][N];
41 int in[N];
42 int ans[N];
43 
44 void khan()
45 {
46     queue<int> Q;
47     for (int i=1;i<=n;i++)
48         if (in[i]==0) Q.push(i);
49     int cnt=0;
50     while (!Q.empty())
51     {
52         int u = Q.front(); Q.pop();
53         ans[cnt++] = u;
54         for (int i=1;i<=n;i++)
55         {
56             if (mp[u][i])
57             {
58                 in[i]--;
59                 if (in[i]==0) Q.push(i);
60             }
61         }
62     }
63     for (int i=0;i<cnt;i++)
64         printf("%d%c",ans[i],i==cnt-1?'\n':' ');
65 }
66 
67 int main()
68 {
69     while (scanf("%d",&n)!=EOF)
70     {
71         memset(mp,0,sizeof(mp));
72         memset(in,0,sizeof(in));
73         for (int i=1;i<=n;i++)
74         {
75             int x;
76             while (1)
77             {
78                 scanf("%d",&x);
79                 if (x==0) break;
80                 mp[i][x]=1;
81                 in[x]++;
82             }
83         }
84         khan();
85     }
86     return 0;
87 }
View Code

 

 

 






posted @ 2016-07-22 18:01  happy_codes  阅读(188)  评论(0编辑  收藏  举报