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 ;
}


M - Ordering Tasks(拓扑排序)的更多相关文章

  1. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

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

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  3. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  4. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  5. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  6. UVA10305 Ordering Tasks (拓扑序列)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398586.html 题意: 假设有N个变量,还有M个二元组(u, v),分别表示变量u 小于 v.那么.所有变量从小到大 ...

  7. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  8. 拓扑排序(Topological Order)UVa10305 Ordering Tasks

    2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...

  9. [拓扑排序]Ordering Tasks UVA - 10305

    拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...

随机推荐

  1. plsql 使用desc命令提示invalid sql statement

    使用SQL WINDOW运行desc命令就报invalid sql statement错误!! 使用Command Window就可以!!!

  2. Ubuntu安装FreeSWITCH亲测

    本人在安装FreeSWITCH的时候遇到了相当多的坑,网上很多方法都模棱两可,经常装失败,最后终于装成功后做一下总结 最顺利的安装方式 1. 下载压缩文件 下载地址:http://files.free ...

  3. EF Core基本使用

    Mysql: nuget 安装 Pomelo.EntityFrameworkCore.MySql Microsoft.EntityFrameworkCore.Design csprj 修改: < ...

  4. Laya的滚动容器

    想实现一个简单的滚动容器.例如水平排列10个图标,可以左右滑动查看的. Egret里有布局容器可以滚动 Laya看了教程和示例,没有找到一个滚动容器,只有一个list,需要设置item,显然不是我想要 ...

  5. win10无法运行phantomjs

    @参考文章 echarts利用phantomjs将 图片保存到电脑,在win7下好好的,在win10下报错: 'phantomjs' 不是内部或外部命令,也不是可运行的程序或批处理文件. 在win7下 ...

  6. tp5 Class 'gmars\rbac\Rbac' not found

    tp强调的是资源的自动加载,自动加载前提是要配置进去,不管是手工还是系统自动配置. 解决方案:查看autoload_psr4.php文件,如果没有的话,是composer的操作哪里有问题.

  7. 【转帖】5G基站建设下的“中国速度”:北上广深领跑全国,均超1万个

    5G基站建设下的“中国速度”:北上广深领跑全国,均超1万个 https://www.laoyaoba.com/html/news/newsdetail?source=pc&news_id=73 ...

  8. [转帖]抢先AMD一步,英特尔推出新处理器,支持LPDDR5!

    抢先AMD一步,英特尔推出新处理器,支持LPDDR5! http://www.eetop.cn/cpu_soc/6946240.html 2019.10 intel的最新技术发展. 近日,知名硬件爆料 ...

  9. Java开发笔记(一百二十五)AWT图像加工

    前面介绍了如何使用画笔工具Graphics绘制各种图案,然而Graphics并不完美,它的遗憾之处包括但不限于:1.不能设置背景颜色:2.虽然提供了平移功能,却未提供旋转功能与缩放功能:3.只能在控件 ...

  10. django下创建多个app,如何设置每个app的urls

    1.创建第二个app 假设我们项目P下面已经有了一个默认的app,名字是app1.现在我想创建第二个app,名字时app2. 进入pychram下的Terminal中,运行命令: python man ...