UVa 10305 (拓扑排序) Ordering Tasks
题意:
经典的拓扑排序。有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列。
分析:
拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系。我们将“小于”这种关系看做一条有向边,如果得到的图是有向无环图DAG(Directed Acyclic Graph),则是存在拓扑排序的,如果存在有向环,则不存在拓扑排序。
注意输入数据里面m可能等于0的情况,就因为这个WA了两次。
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
int G[maxn][maxn], c[maxn];
int topo[maxn], t;
int n, m; bool dfs(int u)
{
c[u] = -;
for(int v = ; v <= n; ++v) if(G[u][v])
{
if(c[v] < ) return false; //´æÔÚ»·
else if(!c[v] && !dfs(v)) return false;
}
c[u] = ; topo[--t] = u;
return true;
} bool toposort()
{
t = n;
memset(c, , sizeof(c));
for(int u = ; u <= n; ++u) if(!c[u])
if(!dfs(u)) return false;
return true;
} int main(void)
{
#ifdef LOCAL
freopen("10305in.txt", "r", stdin);
#endif while(scanf("%d%d", &n, &m) == && n)
{
memset(G, , sizeof(G));
int a, b;
for(int i = ; i < m; ++i)
{
scanf("%d%d", &a, &b);
G[a][b] = ;
}
toposort();
for(int i = ; i < n-; ++i)
printf("%d ", topo[i]);
printf("%d\n", topo[n-]);
} return ;
}
代码君
UVa 10305 (拓扑排序) Ordering Tasks的更多相关文章
- [拓扑排序]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 ...
- Uva 10305 拓扑排序
题意: 给定n个点,与m条边, 给出他们的拓扑排序. 分析: 拓扑排序可以有两种做法, 第一种是dfs, 每次都找到某一个点的终点, 然后加入序列末尾, 正在访问的标记为-1, 访问过的标记为1, 未 ...
- uva 10305 拓扑排序裸题
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...
- uva 1423 拓扑排序
刘书上例题 拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- UVa 1572 (拓扑排序) Self-Assembly
题意: 有n种正放形,每种正方形的数量可视为无限多.已知边与边之间的结合规则,而且正方形可以任意旋转和反转,问这n中正方形是否可以拼成无限大的图案. 分析: 首先因为可以旋转和反转,所以可以保证在拼接 ...
- 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 (拓扑排序裸题)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
随机推荐
- Qt入门之信号与槽机制
一. 简介 就我个人来理解,信号槽机制与Windows下消息机制类似,消息机制是基于回调函数,Qt中用信号与槽来代替函数指针,使程序更安全简洁. 信号和槽机制是 Qt 的核心机制,可以让编程人员将互不 ...
- c++ 时间与字符串转换
.时间转字符串函数 size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timep ...
- ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException
使用BoneCP作为连接池,在启动Tomcat报出以下异常: 一月 02, 2016 2:12:17 下午 org.apache.tomcat.util.digester.SetPropertiesR ...
- 单例模式(.NET)
问题描述: 单例模式 Singleton Pattern 问题解决: (1)单例模式简介: Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个 ...
- 20160727noip模拟赛zld
首先最优策略肯定是这样的:我们取出这个序列中的最大值,然后将整个序列分为左右两部分, 那么我们一定先把左右两部分合起来然后再与这个值合并 那么我们可以得出一个基于最值查询(rmq)的的算法,但是zld ...
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
The Himalayas http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5341 签到 #include<cstdio& ...
- Introduction to Deep Neural Networks
Introduction to Deep Neural Networks Neural networks are a set of algorithms, modeled loosely after ...
- jQuery.validate errorPlacement
在被验证的控件的后一个元素控制显示 errorPlacement: function(error, element) { element.next().css("color",&q ...
- spring mvc 注解 annot失效
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...
- Synchronized Methods
Synchronized Methods The Java programming language provides two basic synchronization idioms: synchr ...