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. NEED TO DO

    任务清单 计算几何  KDtree  容斥  后缀自动机套数据结构 FFT  四边形不等式/决策单调性优化  欧拉路 KM算法  BM算法  数论 min25筛  后缀数组 吉司机线段树 生成函数  ...

  2. python实现简单的百度云自动下载

    最近女同让我帮助从百度云下载200个文件,给了我连接和提取码,这种重复的工作不适合人做写了一个简单的爬虫 #coding=utf-8 ''' 自动填写提取码下载百度云资源 方法: for 读取文件中的 ...

  3. kubernetes云平台管理实战:HPA水平自动伸缩(十一)

    一.自动伸缩 1.启动 [root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu ...

  4. Sass学习第一天

    Sass学习 网站学习地址: Sass中文网:https://www.sass.hk/docs/#t7-3 Airen的博客:https://www.w3cplus.com/preprocessor/ ...

  5. VS2012/2013/2015/Visual Studio 2017 关闭单击文件进行预览的功能

    Visual Studio在2010版本后推出了点击项目管理器预览文件的功能,但是对于配置不咋地的旧电脑总是觉得有点卡,下面是解决方案. 英文版方法:Tools->Options->Env ...

  6. SpringBoot系列: 如何优雅停止服务

    ============================背景============================在系统生命周期中, 免不了要做升级部署, 对于关键服务, 我们应该能做到不停服务完成 ...

  7. [Tex学习笔记]小于等于一个常数乘以...

    偏微分的论文中常用: 小于等于一个常数乘以... 这个要怎么输入呢. 只要输入\lesssim 就能得到 $\lesssim$...哈哈. 以前知道, 但是忘记了. 现在又要用.

  8. [物理学与PDEs]第1章第8节 静电场和静磁场 8.3 静磁场

    1. 静磁场: 由稳定电流形成的磁场. 2. 此时, Maxwell 方程组为 $$\beex \bea \Div{\bf D}&=\rho_f,\\ \rot {\bf E}&={\ ...

  9. 最大熵模型和EM算法

    一.极大似然已经发生的事件是独立重复事件,符合同一分布已经发生的时间是可能性(似然)的事件利用这两个假设,已经发生时间的联合密度值就最大,所以就可以求出总体分布f中参数θ 用极大似然进行机器学习有监督 ...

  10. Luogu P4321 随机漫游

    期望DP要倒着推 Luogu P4321 题意 LOJ #2542 不一定是树,询问点不一定均为1 $Solution$ 设计一个巧妙的DP状态 设$ F(S,x)$表示当前在点$ x$已经走遍了$ ...