拓扑排序 POJ 1049 Sorting It All Out
/*
拓扑排序裸题:有三种情况:
1. 输入时发现与之前的矛盾,Inconsistency
2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环);
flag = -1 表示不确定;return 2 表示拓扑序唯一
3. 其他情况都是 Sorted sequence cannot be determined. */
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int in[MAXN], ans[MAXN];
vector<int> G[MAXN];
bool used[MAXN][MAXN];
int n, m; int TopoSort(void)
{
int flag = ;
memset (in, , sizeof (in));
for (int i=; i<=n; ++i)
{
for (int j=; j<G[i].size (); ++j) in[G[i][j]]++;
} queue<int> Q; int cnt = ;
for (int i=; i<=n; ++i) if (!in[i]) Q.push (i); while (!Q.empty ())
{
if (Q.size () > ) flag = -;
int u = Q.front (); Q.pop ();
ans[++cnt] = u;
for (int i=; i<G[u].size (); ++i)
{
int v = G[u][i];
in[v]--;
if (!in[v]) Q.push (v);
}
} if (cnt != n) return ;
else if (flag == -) return -;
else return ;
} int main(void) //POJ 1049 Sorting It All Out
{
//freopen ("POJ_1094.in", "r", stdin); char s[];
while (scanf ("%d%d", &n, &m) == )
{
if (n == && m == ) break; for (int i=; i<=n; ++i) G[i].clear ();
memset (used, , sizeof (used)); int flag = ;
for (int i=; i<=m; ++i)
{
scanf ("%s", &s);
if (flag) continue; int u = s[] - 'A' + ;
int v = s[] - 'A' + ; if (used[v][u]) {flag = ; printf ("Inconsistency found after %d relations.\n", i); continue;} used[u][v] = true;
G[u].push_back (v); int res = TopoSort ();
if (res == ) {flag = ; printf ("Inconsistency found after %d relations.\n", i); continue;}
else if (res == )
{
flag = ;
printf ("Sorted sequence determined after %d relations: ", i);
for (int i=; i<=n; ++i) printf ("%c", ans[i] + 'A' - );
printf (".\n");
}
} if (!flag) puts ("Sorted sequence cannot be determined.");
} return ;
} /*
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
*/
拓扑排序 POJ 1049 Sorting It All Out的更多相关文章
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- 拓扑排序 POJ 2367
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 拓扑排序 (Topological Sorting)
拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...
- 使用 C# 代码实现拓扑排序
0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. https://www.codeproject.com/Articles/869059/Topological-sorti ...
- [LintCode] 拓扑排序
http://www.lintcode.com/zh-cn/problem/topological-sorting/# 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则 ...
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
[C#]使用 C# 代码实现拓扑排序 目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- [Effective JavaScript笔记]第1条:了解使用的js版本
1997年 正式成为国际标准,官方名称为ECMAScript. 1999年 定稿第3版ECMAScript标准(简称ES3),最广泛的js版本. 2009年 发布第5版即ES5,引入了一些新特性,标准 ...
- Linux常用热键(持续更新)
(这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) --圣诞节怎么过, --略过. 今天装ubuntu的时候把windows覆盖了, 凌乱,TX童 ...
- 【Python】Django支持事务方式
代码: with transaction.atomic(): for i in xrange(int(svc_instance_num)): tmp_fileprotect_svc_instance ...
- 【Spring】Spring系列7之Spring整合MVC框架
7.Spring整合MVC框架 7.1.web环境中使用Spring 7.2.整合MVC框架 目标:使用Spring管理MVC的Action.Controller 最佳实践参考:http://www. ...
- 【Hibernate】Hibernate系列2之Session详解
Session详解 2.1.概述-一级缓存 2.2.操作session缓存方法 2.3.数据库隔离级别 2.4.持久化状态 2.5.状态转换 2.6.存储过程与触发器
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【转】idea 用maven骨架生成项目速度慢的问题
转自:http://9leg.com/maven/2015/02/01/why-is-mvn-archetype-generate-so-low.html 最近从IntelliJ Idea 14的Co ...
- iOS 和 Android 触摸事件传递
先看文章,写得很好 ios 触摸事件传递 http://www.cnblogs.com/Quains/p/3369132.html 另外一篇 http://blog.csdn.net/yongyinm ...
- iOS 网络请求中的challenge
这里有一篇文章,请阅读,感谢作者!http://blog.csdn.net/kmyhy/article/details/7733619 当请求的网站有安全认证问题时,都需要通过 [[challenge ...