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:

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; int n,m;
bool G[][];
int topo[];
int vis[];
int t; bool dfs(int u)
{
vis[u]=-; //标记为正在遍历的
for (int v=;v<=n;v++)
{
if (G[u][v])
{
if (vis[v]==-) return ; //说明组成了环,不能拓扑排序
else if (!vis[v]&&!dfs(v)) return ; //继续遍历未遍历的
}
}
vis[u]=; //标记遍历过了
topo[t--]=u; //输出的就是这个数组
return ;
} bool toposort()
{
t=n;
int i;
for (i=;i<=n;i++) //将所有数都遍历
if (!vis[i]&&!dfs(i)) return ;
return ;
}
int main()
{
int i;
int a,b;
while (scanf("%d%d",&n,&m)&&n+m)
{
memset(G,,sizeof (G));
for (i=;i<m;i++)
{
scanf("%d%d",&a,&b);
G [a][b]=; //记录关系
}
memset(vis,,sizeof(vis));
if (toposort())
{
for (i=;i<n;i++)
printf("%d ",topo[i]);
printf("%d\n",topo[n]);
}
}
return ;
}

Khan

 # include <cstring>
# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N = ;
/**************************/ int n;
int mp[N][N];
int in[N];
int ans[N]; void khan()
{
queue<int> Q;
for (int i=;i<=n;i++)
if (in[i]==) Q.push(i);
int cnt=;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
ans[cnt++] = u;
for (int i=;i<=n;i++)
{
if (mp[u][i])
{
in[i]--;
if (in[i]==) Q.push(i);
}
}
}
for (int i=;i<cnt;i++)
printf("%d%c",ans[i],i==cnt-?'\n':' ');
} int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(mp,,sizeof(mp));
memset(in,,sizeof(in));
for (int i=;i<=n;i++)
{
int x;
while ()
{
scanf("%d",&x);
if (x==) break;
mp[i][x]=;
in[x]++;
}
}
khan();
}
return ;
}


最新文章

  1. JavaScript数组方法reduce解析
  2. 什么时候该用NoSQL?
  3. spring boot/cloud 应用监控
  4. minigui交叉编译整理
  5. BZOJ4120 : [Baltic2015]Editor
  6. java 同步锁方法
  7. The secret code
  8. js添加遮罩层
  9. 完整的拆分nginx访问日志
  10. EventStore的设计思路
  11. Linux下区分物理CPU、逻辑CPU和CPU核数
  12. Android开发之漫漫长途 XVI——ListView与RecyclerView项目实战
  13. .net core 使用MD5加密解密字符串
  14. ionic 3 常见报错及解决办法
  15. mixer中动态Alpha通道处理案例
  16. 使用Fiddler测试WebApi接口
  17. org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column &#39;alarmGroup&#39; from result set. Cause: java.sql.SQLException: Error
  18. Mac python3.6 安装即使用 psycopg2
  19. Fiddler 简单介绍
  20. excel 单元格0 不显示的最佳方法

热门文章

  1. 爬虫urllib2库的基本使用
  2. Python3基础 函数 无return、return 空或None 的效果相同
  3. springMVC和struts2有什么不同?为什么要用springMVC或者struts2?让你实现一个MVC框架大概如何设计?
  4. jquery click 与原生 click 的区别
  5. 安卓 android studio 报错 The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle
  6. 10分钟完成一个最最简单的BLE蓝牙接收数据的DEMO
  7. SpringBoot系列教程JPA之update使用姿势
  8. NGINX安全配置和限制访问
  9. 如何配置STP
  10. LeetCode 75. 颜色分类(Sort Colors) 30