Ordering Tasks UVA - 10305 图的拓扑排序
/**
题目:Ordering Tasks UVA - 10305
链接:https://vjudge.net/problem/UVA-10305
题意:给定一个有向无环图,求拓扑序列。
思路:
对一条链,从后往前存入到数组的头部。
如:5->4->3->2; 那么存到数组为: a[] = {5,4,3,2}; 其他链要么和这条链尾部有交集,其他没有交集。由于交集处已经存进去了,为交集存入的肯定是放在数组的头部。
这样可以保证。 如果存在环,不存在拓扑序列。
*/ #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e2+;
const double eps = 1e-;
int topo[maxn], c[maxn], z;
int G[maxn][maxn];
int n, m;
bool dfs(int u)
{
c[u] = -;
for(int i = ; i <= n; i++){
if(G[u][i]==) continue;
if(c[i]==-) return false;///如果存在环,那么返回false;因为-1表示这条链还没寻找结束,
///如果一直寻找,找到了原来出现过的,那么存在环。
if(c[i]==&&G[u][i]){
if(dfs(i)==false) return false;
}
}
topo[z-] = u;
c[u] = ;
z--;
return true;
}
bool topoSort()
{
memset(c, , sizeof c);
z = n;
for(int i = ; i <= n; i++){
if(c[i]==){
if(dfs(i)==) return false;
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)==&&(n+m))
{
int u, v;
memset(G, , sizeof G);
for(int i = ; i < m; i++){
scanf("%d%d",&u,&v);
G[u][v] = ;
}
topoSort();
printf("%d",topo[]);
for(int i = ; i < n; i++){
printf(" %d",topo[i]);
}
printf("\n");
}
return ;
}
Ordering Tasks UVA - 10305 图的拓扑排序的更多相关文章
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
- 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。
题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...
- Ordering Tasks UVA - 10305(拓扑排序)
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...
- 拓扑排序 (Ordering Tasks UVA - 10305)
题目描述: 原题:https://vjudge.net/problem/UVA-10305 题目思路: 1.依旧是DFS 2.用邻接矩阵实现图 3.需要判断是否有环 AC代码 #include < ...
- 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序
题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足: Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆. 现在 ...
- 算法87-----DAG有向无环图的拓扑排序
一.题目:课程排表---210 课程表上有一些课,是必须有修学分的先后顺序的,必须要求在上完某些课的情况下才能上下一门.问是否有方案修完所有的课程?如果有的话请返回其中一个符合要求的路径,否则返回[] ...
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- Paint the Grid Again (隐藏建图+优先队列+拓扑排序)
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...
- 图的拓扑排序,AOV,完整实现,C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
随机推荐
- 关于scanf函数的返回值问题
如: scanf("%d%d",&a,&b); 1.如果a和b都被成功读入,则scanf的返回值为2 2.如果只有a被成功读入,那么返回值为1 3.如果a和b都未被 ...
- iOS开发——使用Autolayout弹出键盘
参考: http://segmentfault.com/q/1010000002420050 http://blog.csdn.net/qq448631961/article/details/4034 ...
- JVM OQL查询语言
OQL查询语言 SELECT Clause The SELECT clause determines what to extract from the heap dump. To display ob ...
- ubuntu_software_install
1.atom PPA安装 命令行上依次输入即可完成安装: sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo a ...
- docker_usb开发软件部署
1.docker镜像包 (备注:61提供,带桌面版本) rayosx2.0.2.tar 2.paho-mqtt dnf install git -y git clone https://github ...
- [Spring boot] Integrating with h2 database
In pom.xml add dependency: <dependencies> <dependency> <groupId>org.springframewor ...
- web.xml文件中配置mime下载文件类型(转)
转自:http://5aijava.iteye.com/blog/166600 TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不 ...
- Activemq消息确认机制 --转载
转自:http://blog.csdn.net/czp11210/article/details/47022639 ActiveMQ消息传送机制以及ACK机制详解 AcitveMQ是作为一种消息存 ...
- There is insufficient memory for the Java Runtime Environment to continue问题解决
在linux系统下长时间进行性能測试,连续几次发生server假死无法连接上的情况,无奈仅仅能重新启动server.在測试路径下发现hs_err_pid17285.log文件,打开文件查看其主要内容例 ...
- openerp-server.conf 中配置 dbfilter 参数无效的解决办法
来自:http://shine-it.net/index.php/topic,14517.html 以前就发现过这个问题, 今天重新在群里同大家讨论了一下. 有时候可能我们希望用户不从登陆界面的账套选 ...