Poj 1094 拓扑排序Kahn

Sorting It All Out

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 41332 Accepted: 14478

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

题目大意:给你多组边让你判断是否能够确定一个拓扑排序,这也就意味着我们的排序只能有唯一的一种,不能有多种方案,也就是说这里并不保证给我们的是一个DAG图。而要求我们自己判断

这里就要用到拓扑排序的一个经典算法Kahn:

摘一段维基百科上关于Kahn算法的伪码描述:

L← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
foreach node m with an edge e from nto m do
remove edge e from thegraph
ifm has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least onecycle)
else
return L (a topologically sortedorder)

不难看出该算法的实现十分直观,关键在于需要维护一个入度为0的顶点的集合:

每次从该集合中取出(没有特殊的取出规则,随机取出也行,使用队列/栈也行,下同)一个顶点,将该顶点放入保存结果的List中。

紧接着循环遍历由该顶点引出的所有边,从图中移除这条边,同时获取该边的另外一个顶点,如果该顶点的入度在减去本条边之后为0,那么也将这个顶点放到入度为0的集合中。然后继续从集合中取出一个顶点…………

当集合为空之后,检查图中是否还存在任何边,如果存在的话,说明图中至少存在一条环路。不存在的话则返回结果List,此List中的顺序就是对图进行拓扑排序的结果。

代码如下:

#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 27;
int n,m;
vector<int> G[maxn];
int in[maxn],deg[maxn];
char ans[maxn];
int cnt;
void init() {for(int i = 0;i < n;i++) G[i].clear();memset(in,0,sizeof(in));
}
bool check(int u,int v) {for(int i = 0;i < G[u].size();i++) if(G[u][i] == v) return true;return false;
}
//传入当前边数
int topsort(int s) {for(int i = 0;i < n;i++) deg[i] = in[i];cnt = 0;int flag = 1;memset(ans,0,sizeof(ans));queue<int> Q;while(!Q.empty()) Q.pop();for(int i = 0;i < n;i++) {if(deg[i] == 0) Q.push(i);}//if(Q.size() > 1) flag = 0; !!!判断是否有多种情况while(!Q.empty()) {if(Q.size() > 1) flag = 0; int u = Q.front();Q.pop();ans[cnt++] = u + 'A';int len = G[u].size();for(int i = 0;i < len;i++) {int v = G[u][i];deg[v]--;s--;if(deg[v] == 0) {Q.push(v);}}}if(s != 0) return 2;else if(cnt == n && flag) return 3;else return 1;
}
int main() {while(scanf("%d%d",&n,&m) == 2) {if(n == 0 && m == 0) break;init();char s[5];int flag,ok = 0,k;for(int i = 0;i < m;i++) {scanf("%s",s);if(ok) continue;int u = s[0] - 'A',v = s[2] - 'A';if(check(u,v)) continue;in[v]++;G[u].push_back(v);flag = topsort(i+1);if(flag == 3) {ok = 1;printf("Sorted sequence determined after %d relations: %s.\n",i+1,ans);}else if(flag == 2) {ok = 1;printf("Inconsistency found after %d relations.\n",i+1);}}if(flag == 1) printf("Sorted sequence cannot be determined.\n");}return 0;
}

转载于:https://www.cnblogs.com/pot-a-to/p/11119197.html

Poj 1094 拓扑排序Kahn相关推荐

  1. POJ 1094 拓扑排序

    题意大坑,建议先看Discuss-- 否则代码写得就像以下的一团糟.... 其实并不难,拓扑排序+乱搞就可以AC. // by SiriusRen #include <cstdio> #i ...

  2. poj 4084:拓扑排序

    poj 4084:拓扑排序 非常好的题目,恶心的算法 描写叙述 给出一个图的结构,输出其拓扑排序序列,要求在同等条件下.编号小的顶点在前. 输入 若干行整数,第一行有2个数,分别为顶点数v和弧数a,接 ...

  3. 拓扑排序----Kahn算法和字典序最小的拓扑排序

    一.拓扑排序定义: 二.卡恩算法(Kahn): 1.Kahn算法介绍: 有向无环图DAG至少具有一个度数为0的顶点和一个度数为0的顶点. 证明:上述事实有一个简单的证明,即DAG不包含循环,这意味着所 ...

  4. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  5. (数据结构)有向图的拓扑排序 Kahn算法

    拓扑排序是对有向无圈图的顶点的一种排序,使得如果存在一条从vi到vj的路径,那么在排序中,vi必须出现在vj的前面. 首先,我们需要准备一个vector<int> link[maxn],l ...

  6. DAG拓扑排序-Kahn算法

    拓扑排序就是对一个有向无环图进行排序,使其变成一个线性关系,并且保证其前后的位置关系不改,简言之,就是把一个偏序变成一个全序(线性序). 拓扑排序有两种算法,一种是借助DFS排序,另一种是卡恩算法,这 ...

  7. 拓扑排序(Kahn算法和基于DFS求解法)

    拓扑排序是对有向无环图(DAG)进行排序,从而找到一个序列.该序列满足对于任意一对不同的顶点u,v∈V,若G中存在一条从u->v的边,则在此序列中u在v前面. 拓扑排序也可以用来判断一个有向图是 ...

  8. pku 1094(拓扑排序,多次拓扑)

    View Code /*Name: 拓扑排序,多次拓扑Copyright: Author: Try86Date: 16/04/12 21:36Description: */#include <c ...

  9. 【题解】(图论) —— POJ 0719:拓扑排序

    题目链接:OpenJudge - 0719:拓扑排序 总时间限制: 10000ms 内存限制: 1000kB 描述 给出一个图的结构,输出其拓扑排序序列,要求在同等条件下,编号小的顶点在前 输入 若干 ...

最新文章

  1. STM32-RCC内部总线时钟设置程序详讲
  2. 【深度学习】基于深度神经网络进行权重剪枝的算法(一)
  3. Pytorch模型层简单介绍
  4. layui表格固定列覆盖滚动条导致错位
  5. C#通过反射动态加载dll,读取module、类、方法、特性,并通过反射+简单工厂+配置文件 实现ioc
  6. vue页面按钮点击后,呈现loading加载状态
  7. java堆是gc管理_JVM内存管理及GC机制
  8. 【数据结构和算法笔记】二叉树的概念和性质
  9. 架构之美第四章-架构与美
  10. 用AD9画51单片机的最小系统
  11. WPS金山软件,死磕微软31年,故事要从1988年说起...
  12. 基、维数和坐标 过渡矩阵与坐标变换
  13. 计算机输入设备的作用,认识计算机输入设备—键盘
  14. Android调建行APP、建行微信遇到的坑
  15. 巡逻机器人(Patrol Robot, Uva1600)
  16. 如何开启 vue 项目
  17. 剑指核心-TaoCloud全闪SDS助力构建高性能云服务
  18. UE4/UE5 多线程开发 附件插件下载地址
  19. java软件工程师就业招聘信息_Java软件工程师就业前景为什么这么好呢?
  20. Samba服务+Samba实验

热门文章

  1. Python 之 字典dic(Python19)
  2. Dynamic Few-Shot Visual Learning without Forgetting
  3. 【论文详读】Overcoming catastrophic forgetting in neural networks
  4. Python统计学:如何理解单样本t检验?
  5. 【渝粤题库】陕西师范大学200691离散数学作业(高起专、高起本、专升本)
  6. 增强现实(AR:Augmented Reality ) 之介绍及应用
  7. html文件桌面图标空白,文件图标变空白是怎么回事
  8. 黑马程序员_7k面试题之交通灯
  9. 关于output.topk
  10. web3j contract 使用方法