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. 题意:n个字母,m个不等式,从1到m个不等式,问到第几个不等式的时候能确定字母间的大小关系,或者会出现矛盾(拓扑环),如果到m个不等式都无法确定,那就是无序的。::
注:这个从1到m是题目中没有给出的,但是题目确实是判断最少不等式确定有序,或者确定矛盾,如果都无法确定才认为是无序的。 思路:其实主要就是拓扑排序,拓扑排序过程中,如果没有入度为0的点,那么就存在环,就是矛盾的,floyd可有可无。
如果出现多个零点进入队列,那么就是无序的,但是无序优先级最低,所以不能立即退出,需要继续拓扑排序,判断完所有点入度情况,看看是否存在矛盾。
至于加入的floyd,由于floyd可以直接处理传递关系,就可以直接判出是否存在矛盾,然后,如果这时候再出现无序就能直接退出。 (若不使用floyd,代码如注释)
 #include<queue>
#include<cstdio>
#include<cstring> using namespace std; int n,m; int maps[][];
struct Node
{
int a,b;
int val;
Node(int a=,int b=,int val=):a(a),b(b),val(val) {}
} node[]; int ans[];
bool topsort()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
maps[i][j] |= maps[i][k] & maps[k][j];
}
}
}
for(int i=;i<=n;i++)if(maps[i][i])return ;
return ;
} int check()
{
if(topsort())return -;
int ind[];
memset(ind,,sizeof(ind));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(maps[i][j])
{
ind[j]++;
}
}
}
int tot,top,flag=;
for(int i=;i<=n;i++)
{
tot = ;
for(int j=;j<=n;j++)
{
if(!ind[j])
{
tot++;
top = j;
}
}
if(tot >= ) return ;
// if(tot >= 2)flag = 0;
// if(!tot)return -1;
ans[i] = top;
ind[top] = -;
for(int i=;i<=n;i++)
{
if(maps[top][i])ind[i]--;
}
}
// return flag;
return ;
} int main()
{
while(~scanf("%d%d",&n,&m)&&n&&m)
{
int flag = ;
memset(maps,,sizeof(maps));
char a,b,c;
for(int i=; i<=m; i++)
{
scanf(" %c %c %c",&a,&b,&c);
if(b == '<')
maps[a-'A'+][c-'A'+] = ;
else
maps[c-'A'+][a-'A'+] = ;
if(!flag)
{
flag = check();
if(flag == )
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=; j<=n; j++)
printf("%c",ans[j]+'A'-);
puts(".");
}
else if(flag == -)
{
printf("Inconsistency found after %d relations.\n",i);
}
}
if(i == m && !flag)
printf("Sorted sequence cannot be determined.\n");
}
}
}

Sorting It All Out (拓扑排序+floyd)的更多相关文章

  1. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  2. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  3. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  4. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  5. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  6. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

  7. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  8. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  9. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

随机推荐

  1. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  2. centos7 LNMP

    Nginx1.13.5 + PHP7.1.10 + MySQL5.7.19 一.安装Nginx 1.安装依赖扩展 # yum -y install wget openssl* gcc gcc-c++ ...

  3. Caused by: java.lang.ClassNotFoundException: org.springframework.web.filter.FormContentFilter

    又是一个报错,我写代码真的是可以,所有的bug都会被我遇到,所有的问题我都能踩一遍,以前上学的时候同学就喜欢问我问题,因为他们遇到的问题,我早就遇到了......... 看看报错内容: 2019-04 ...

  4. C# 另一种提交表单

    一般提交表单的方式就是:Get,Post 以及关联action 今天看了一种方式感觉不错: 可以在submit里面写 PostBackUrl="XXXX",即回发的URL,可以实现 ...

  5. ACM-ICPC 2018 沈阳赛区网络预赛 I Lattice's basics in digital electronics(模拟)

    https://nanti.jisuanke.com/t/31450 题意 给出一个映射(左为ascll值),然后给出一个16进制的数,要求先将16进制转化为2进制然后每9位判断,若前8位有奇数个1且 ...

  6. Nginx模块

    模块概述 https://kb.cnblogs.com/page/98352/ Nginx模块工作原理概述 (Nginx本身支持多种模块,如HTTP模块.EVENT模块和MAIL模块,本文只讨论HTT ...

  7. @RequestParam,@PathParam,@PathVariable等注解区别

    @RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @P ...

  8. 【js课设】电子画板01

    这学期web开发课的课设选了电子画板课题.(人家本来想做富文本编辑器的嘛然鹅老师在第二版里把这题删掉了。゚ヽ(゚´Д`)ノ゚。) 主要考虑的有[界面美观][画笔类型][画布分层]这三个点了. [界面美 ...

  9. KNN和Kmeans聚类有什么不同?

    这两种算法之间的根本区别是,Kmeans本质上是无监督学习而KNN是监督学习.Kmeans是聚类算法,KNN是分类(或回归)算法. Kmeans算法把一个数据集分割成簇,使得形成的簇是同构的,每个簇里 ...

  10. 目标检测网络之 YOLOv3

    本文逐步介绍YOLO v1~v3的设计历程. YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这 ...