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. mongoDB 数据库操作

    mongoDB 数据库操作 数据库命名规则 . 使用 utf8 字符,默认所有字符为 utf8 . 不能含有空格 . / \ "\0" 字符 (c++ 中会将 "\0&q ...

  2. ajax 跨域 springboot

    CORS 定义 Cross-Origin Resource Sharing(CORS)跨来源资源共享是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略,是 ...

  3. python list的使用

    list列表的常用方法有: #!/usr/bin/env python # -*- coding:utf- -*- #以下方法全在python2..x版本运行,请3.x以上的小伙伴们在print(放入 ...

  4. solr的基础和安装

    下载地址 http://archive.apache.org/dist/lucene/solr/   推荐 http://www.apache.org/dyn/closer.lua/lucene/so ...

  5. OpenStack VS Kubernetes,谁是你心中的王者?

      当下云计算的领域里热度最高的两个项目,无疑是OpenStack和Kubernetes.如果云计算是一个风起云涌的江湖,毫不夸张的说OpenStack和Kubernetes就是江湖里的泰山北斗.Op ...

  6. SSH框架之hibernate《二》

    Hibernate第二天     一.hibernate的持久化类和对象标识符         1.1持久化类的编写规范             1.1.1什么是持久化类:               ...

  7. vue中使用swiper-slide时,循环轮播失效?

    前言 vue 项目中使用时,组件swiper-slide 如果用v-for循环的话,loop:true 就不能无缝轮播,每次轮播到最后一张就停止了??? 正文 代码如下: <swiper :op ...

  8. Python3:排序函数sort() 和 sorted() 之介绍

    今天来讲一下Python中的排序函数.Python中有2个内建的排序函数,分别为sort() 和 sorted() 下面介绍分别介绍一下2个函数: 1.有一个列表 :a=[1,4,5,88,0,7], ...

  9. python3 使用pip安装(命令行中)失败或 “not a supported wheel” 解决方案!

    原因1: 安装的不是对应python版本的库,下载的库名中cp36代表python3.6,其它同理. 原因2:(我遇到的情况----下载的是对应版本的库,然后仍然提示不支持当前平台) 百度了一下,说法 ...

  10. 爬虫工程师JD归纳

    核心能力归纳 负责:多平台信息的抓取,清洗和分析工作 要求: 熟悉常用开源爬虫框架,如 scrapy / pyspider
 了解基于Cookie的登录原理,熟悉常用的信息抽取技术,如正则表达式.XP ...