输入n,m,n代表点数,m代表边数(i,j),排序时i在j前面,没出现的点随意排

#include <iostream>
#include<stdio.h>
#include<math.h>
#include<memory.h>
using namespace std; const int maxNum = 120;
int a, b;
int map[maxNum][maxNum];
int vis[maxNum];
int index2;
int res[maxNum];
bool topoSort(int row)
{
vis[row] = -1;
for (int i = 1; i <= a; i++)
{
if (vis[i] == -1 && i != row)
continue;
else if (map[row][i] == 1 && vis[i] == 0)
{
topoSort(i);
}
}
res[++index2] = row;
vis[row] = 1;
return true;
} int main()
{ while (cin >> a >> b)
{
if(a == b && b == 0)
{
return 0;
}
index2 = 0;
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis));
int j, k;
for (int i = 0; i < b; i++)
{
cin >> j >> k;
//前向边
map[j][k] = 1;
} for (int i = 1; i <= a; i++)
{
if (vis[i] == 0)
topoSort(i);
}
for (int i = a; i >= 1; i--)
{
if (i == a)
{
cout << res[i];
continue;
}
cout << " " << res[i];
}
cout << endl;
}
return 0;
}

  

uva-10305-水题-拓扑排序的更多相关文章

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

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

  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. UVA.10305 Ordering Tasks (拓扑排序)

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

  4. Ordering Tasks UVA - 10305 图的拓扑排序

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

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

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

  6. C#LeetCode刷题-拓扑排序

    拓扑排序篇 # 题名 刷题 通过率 难度 207 课程表   40.0% 中等 210 课程表 II   39.8% 中等 329 矩阵中的最长递增路径   31.0% 困难 ​​​​​​​

  7. UVA 1572 Self-Assembly(拓扑排序)

    1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...

  8. UVa 1595 (水题) Symmetry

    颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...

  9. UVa 10391 (水题 STL) Compound Words

    今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...

  10. UVa 1572 Self-Assembly (拓扑排序)

    题目链接: https://cn.vjudge.net/problem/UVA-1572 Automatic Chemical Manufacturing is experimenting with ...

随机推荐

  1. [LeetCode&Python] Problem 868. Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  2. 你在AutoHotKey面前居然敢比调音量 - imsoft.cnblogs

    当你正在电脑游戏中酣战之际.或者正沉浸在动作大片紧张激烈的情节中.或者正在全神贯注的聆听优美动听音乐……,在这些场景中,如果你需要迅速对音量进行调节(例如增大减小音量,或者静音)怎么办?难道返回Win ...

  3. 《DSP using MATLAB》 Problem 4.9

    代码: %% ---------------------------------------------------------------------------- %% Output Info a ...

  4. Struts2重新学习之自定义拦截器(判断用户是否是登录状态)

    拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...

  5. Python & 机器学习入门指导

    Getting started with Python & Machine Learning(阅者注:这是一篇关于机器学习的指导入门,作者大致描述了用Python来开始机器学习的优劣,以及如果 ...

  6. C# NPOI导出Excel和EPPlus导出Excel

    转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...

  7. 关联容器map(红黑树,key/value),以及所有的STL容器详解

    字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s=“a1a2···an”(n>=0).它是编程语言中表示文本的数据类型.在程序设计中,字符串(string)为符号或数 ...

  8. StreamSets sdc rpc 测试

    一个简单的参考图 destination pipeline 创建 pipeline flow sdc destination 配置 origin sdc rpc pipeline pipeline f ...

  9. PHP的extension_dir设置问题

    PHP安装时,extension_dir的路径要设成绝对路径:extension_dir = "D:/Tools/php-7.0.5/ext", 不然如果设成extension_d ...

  10. HIVE之 Sqoop 1.4.6 安装、hive与oracle表互导

    1. sqoop数据迁移 1.1 概述 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HI ...