uva-10305-水题-拓扑排序
输入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-水题-拓扑排序的更多相关文章
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVa 10305 - Ordering Tasks (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- UVA 10305 Ordering Tasks(拓扑排序的队列解法)
题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...
- C#LeetCode刷题-拓扑排序
拓扑排序篇 # 题名 刷题 通过率 难度 207 课程表 40.0% 中等 210 课程表 II 39.8% 中等 329 矩阵中的最长递增路径 31.0% 困难
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- UVa 1595 (水题) Symmetry
颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...
- UVa 10391 (水题 STL) Compound Words
今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...
- UVa 1572 Self-Assembly (拓扑排序)
题目链接: https://cn.vjudge.net/problem/UVA-1572 Automatic Chemical Manufacturing is experimenting with ...
随机推荐
- RequireJs 与 SeaJs的相同之处与区别
相同之处: RequireJS 和 Sea.js 都是模块加载器,倡导模块化开发理念,核心价值是让 JavaScript 的模块化开发变得简单自然. 不同之处: 定位有差异.RequireJS 想成为 ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- sys目录下常用文件汇总
1. /sys/kernel/debug/gpio 可以实时反馈系统中感兴趣的gpio引脚的状态 root@g6s:/sys/kernel/debug# cat gpio gpiochip7: GPI ...
- ZH奶酪:最简单的Django安装方法(Windows 7)
前提是你已经在机器上安装了python~并且你的机器能够上网~ 1.进入:https://bootstrap.pypa.io/get-pip.py,ctrl+s保存网页(其实是.py文件)到某一目录: ...
- task optimization之superglue分析
开启logging (例子F:\wamp\www\git_repos\GitHub\GeneralUtility\superglue-master\examples\src\logging.cpp) ...
- MySQL中UNSIGNED和ZEROFILL的介绍
UNSIGNED: 将数字类型无符号化,这与C和C++这些程序语言的unsigned含义相同. INT的类型范围-2 147 483 648~2 147 483 647 INT UNSIGNED范围0 ...
- chrome 小技巧:保持元素的hover状态
审查元素,选中需要hover的标签 点击"Styles"菜单中的":hov",弹出 Force element state 选中相应的 :hover :acti ...
- Angular 4 延缓加载组件
1. 创建App ng new lazySample --routing 在app组件中的定义路由 2. 创建“Lazy” Module ng g module lazy --flat ng g co ...
- linux非root用户执行开机启动程序
问题 开机启动其他用户的程序或者说非root用户执行开机启动 编写开机启动脚本 编写开机启动脚本apple_tree,放到/etc/init.d,系统启动时会自动执行. 例如,/etc/init.d/ ...
- Go随机数的使用
随机数使用比较广泛,例如,抽奖.均衡等等. 下面简单说明其使用方法. Example1 package main import ( "log" "math/rand&qu ...