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. gradle 使用maven repository 的设置

    repositories {    //Maven中心库(http://repo1.maven.org/maven2)    mavenCentral() //本地库,local repository ...

  2. Oralce 如何将查询结果中的0转成空的

    我们遇到过大多的情况的需求是查询结果中空转为0,这个可以通过oracle的NVL()函数就可以搞定. 之前做报表客户有个需求,查询出结果为0 要转成空的,不显示0 那么在oracle有没有现成函数能搞 ...

  3. Robot Framwork关键字驱动+RedwoodHQ安装

    一.Robot Framwork介绍 Robot Framwork是一款python编写的功能框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行. ...

  4. LeetCode_412. Fizz Buzz

    412. Fizz Buzz Easy Write a program that outputs the string representation of numbers from 1 to n. B ...

  5. node.js使用markdown-it批量转md内容为html

    代码如下: var fs = require('fs'); var MarkdownIt = require('markdown-it'), md = new MarkdownIt(); /* pag ...

  6. LeetCode的一道题引申的python实现的对字符串进行分词,提取词频的方法

    在LeetCode上刷一道题,题目如下: 3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的?最长子串?的长度. 示例?1: 输入: "abcabcbb"输出 ...

  7. PHPExcel 中文使用手册详解 二

    $objPHPExcel = new \PHPExcel(); //定义配置 $topNumber = 2;//表头有几行占用 $xlsTitle = iconv('utf-8', 'gb2312', ...

  8. MinGW ,GNU 是什么

    MinGW : Minimalist GNU for Windows MinGW(Minimalist GNU For Windows)是个精简的Windows平台下的 C/C++.ADA及Fortr ...

  9. 用Python递归做个多层次的文件执行

    想用 递归实现多层次的 '.py'执行但是发现好像不能 import os def func(path): if os.path.isdir(path): for name in os.listdir ...

  10. Python-13-模块和包

    一.模块的概念 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代 ...