G - Ordering Tasks

题目:

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 mn 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个任务要做, 不幸的是,这些任务并不是独立的,执行某个任务之前要先执行完其他相关联的任务。

c++代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int n,m;
int one[105];
int two[105];
int degree[105];
int book[105];//表示他们都没有输出
int main()
{while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0) break;memset(degree,0,sizeof(degree));memset(book,0,sizeof(book));int i,j;int x,y;for(i=1;i<=m;i++){scanf("%d%d",&x,&y);degree[y]++;one[i]=x;two[i]=y; }int k=0;int flag;for(int i = 1;; i=(i+1)%n){if(i==0)i=n;flag=0;if(degree[i]==0&&book[i]==0){book[i]=1;flag=1;for(int j=1;j<=m;j++){if(one[j]==i)degree[two[j]]--;    } } if(flag){k++;if(k<n)printf("%d ",i);else if(k==n){printf("%d\n",i);break;     }  }}}return 0;
} 

G - Ordering Tasks(拓补排序)相关推荐

  1. 51nod-生产口罩(拓补排序+DP)by zyz

    题目:生产口罩 链接:http://class.51nod.com/Classes/Problem.html#courseProblemId=1718&classId=129 //注:题目来自 ...

  2. 士兵排队问题(拓补排序)(附简要拓补排序思想及算法)

    题目描述 有N个士兵(1<=N<=100),编号依次为1,2,...,N.队列训练时,指挥官要把士兵从高到矮排成一行,但指挥官只知道"1 比2 高,7 比 5高"这样的 ...

  3. 【BZOJ3036】绿豆蛙的归宿 拓补排序+概率

    [BZOJ3036]绿豆蛙的归宿 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度. ...

  4. 奖金(拓补排序的应用)

    谁应该高谁的入度++,并记录下谁比低的高(低的得出度),所以入度为零的就是最低的(好不公平),找出所有最低的,将他们算作一层,奖金++(算是拓补排序吧) #include<cstdio> ...

  5. HDU4324 - Triangle LOVE 拓补排序

    HDU4324 - Triangle LOVE : http://acm.showproblemhdu.edu.cn/.php?pid=4324 标准的拓补排序,上代码 : #include < ...

  6. leetcode *210. 课程表 II(拓补排序)(2020.5.17)

    [题目]*210. 课程表 II 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们 ...

  7. Aov网络与拓补排序的实现

    测试的节点分布如下: 测试代码如下: /** 拓补排序的实现,使用邻接链表存储有向图 */ #include <iostream> #include <cstdio> #inc ...

  8. 每日一题30:拓补排序

    问题描述 所谓拓补排序就是确定图中节点的一种顺序,使得某些在别的节点访问之前不能访问到的节点排在后面.所以该算法的核心是每一步选择一个没有入度的节点,因为没有入度意味着该节点没有前驱,得到一个节点后, ...

  9. 后缀自动机求多个串的最长公共子串+拓补排序讲解+LCS2 - Longest Common Substring II

    网上所有关于后缀自动机拓补排序的文章,都默认读者会拓补排序,简直了. 后缀自动机的拓补排序,就是按照长度进行排序,在进行特定操作的时候,通过较长的后缀来更新较短的后缀.那么也就是通过拓补排序中排名靠后 ...

  10. POJ 1094拓补排序

    POJ 1094拓补排序问题,需要加一些判断 #include <cstdio> #include <queue> #include <vector> #inclu ...

最新文章

  1. 什么是L1/L2/L3 Cache?
  2. 页面可视化搭建工具前生今世
  3. delphi中的dbgrid使用
  4. linux apache 文件服务器,Linux下搭建Apache服务器全过程详解
  5. 海量java等互联网相关电子图书分享
  6. dpdk18.11 收发包流程分析
  7. 计算机user用户的密码存储在,windows存放用户密码的文件
  8. java file的相对路径_JAVA File的建立及相对路径绝对路径
  9. CORBA版HelloWorld
  10. 超火动态表白H5源码
  11. android 不透明度
  12. Introduce·艺术学超级友好必投普通期刊推荐之《戏剧之家》
  13. 给宝贝女儿雅琪写的顺口溜
  14. swift airdrop_适用于Android的AirDrop:如何使用附近的Android分享
  15. oracle sql查数据是否有重复
  16. mysql错误42000_mysql5.7 异常ERROR 1055 (42000)
  17. 利用ArcGIS提取高光谱图像每个像素的光谱信息,再利用matlab显示每个像素的光谱信息
  18. GEA 3.1 重温C++以及实践
  19. 大数据 -- 数据倾斜
  20. Ubuntu14联合安装opencv与opencv_contrib踩过的坑

热门文章

  1. dubbo学习--源码结构概述(5)
  2. Springmvc 的post请求的json格式参数
  3. CSS3魔法堂:说说Multi-column Layout
  4. sys.dm_db_wait_stats
  5. Centos 5.8 搭建ntp服务器
  6. 原声socket 向服务端发长连接
  7. C#保存CookieContainer到文件
  8. mysql主从同步完整命令
  9. ehcache缓存共享(rmi方法)
  10. 微软Exchange Server 2010 SP1下载