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. 题意:给一些大写字母的偏序关系。判断是否存在唯一的关系,或者他们的关系是矛盾的,即造成了回路,或者到最后不能确定他们的关系。

用拓扑排序:

1.拓扑排序可以用栈来实现,每次入栈的是入度为0的节点。

1.拓扑排序的结果一般分为三种情况:1、可以判断 2、有环出现了矛盾 3、条件不足,不能判断.

2.这道题不仅需要判断这三种情况,而且还要判断在处理第几个关系时出现前两种情况,对于本道题来说三种情况是有优先级的。前两种情况是平等的谁先出现先输出谁的相应结果,对于第三种情况是在前两种情况下都没有的前提下输出相应结果的.

 #include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int maxn=;
int n,m;
int pb[maxn][maxn],rd[maxn],list[maxn];
int toposort()
{
int i,j=;
int in[maxn];
memcpy(in,rd,sizeof(rd)); //复制入度数组,以防改变原数组
stack <int> s;
for(i=; i<n; i++) //入度为0的入栈
{
if(!in[i])
s.push(i);
}
bool flag=false;
while(!s.empty())
{
if(s.size()>)
flag=true;
int t=s.top(); // 记录出栈数字
s.pop();
list[j++]=t;
for(i=; i<n; i++)
{
if(pb[t][i])
{
in[i]--; //入度减一
if(!in[i])
s.push(i); //入度为0的继续入栈
}
}
}
if(j!=n) //存在回路
return ;
else if(flag) //有多重排序方式
return ;
return ;
}
int main()
{
while(cin>>n>>m,n||m)
{
memset(pb,,sizeof(pb));
memset(rd,,sizeof(rd));
char a,b;
int frist,scend;
frist=scend=;
for(int i=; i<=m; i++)
{
getchar();
scanf("%c<%c",&a,&b);
if(!frist && !scend)
{
if(pb[b-'A'][a-'A']==) //存在反向边
{
scend=;
printf("Inconsistency found after %d relations.\n",i);
continue;
}
if(!pb[a-'A'][b-'A'])
{
pb[a-'A'][b-'A']=;
rd[b-'A']++;
}
int res=toposort(); //进行拓扑排序
if(res==) //序列能够唯一被确定
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=; j<n; j++)
printf("%c",list[j]+'A');
printf(".\n");
frist=;
}
else if(res==) //存在回路
{
scend=;
printf("Inconsistency found after %d relations.\n",i);
}
} }
if(!frist&&!scend) //即不能唯一约定,又不构成回路
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

poj 1094 Sorting It All Out 拓补排序的更多相关文章

  1. POJ 1094 Sorting It All Out【拓扑排序】

    题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...

  2. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

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

  3. POJ 1094 Sorting It All Out【拓扑排序 / 比较字母大小】

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

  4. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

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

  5. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

  6. POJ 1094 Sorting It All Out 【拓扑排序】

    <题目链接> 题目大意: 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序. 解题分析 ...

  7. POJ - 1094 Sorting It All Out(拓扑排序)

    https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...

  8. 题解报告:poj 1094 Sorting It All Out(拓扑排序)

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

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

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

随机推荐

  1. mysql 查看mysql相关信息

    登入数据库的时候: select @@version; select version(); 复制代码 mysql> select @@version; +-----------+ | @@ver ...

  2. Oracle性能优化4-索引

    Oracle优化可以分为通过改写sql优化和不改写sql优化不改写sql优化一般通过index来实现 在Oracle数据库中,索引按照索引机制的不同,可以分为三种. 1. B-Tree索引 B-Tre ...

  3. Oracle_SQL(7) 复杂查询

    1.rownum 伪列<,<=select * from emp where rownum<5; 取工资前3名的人员 select * from (select * from emp ...

  4. 关于java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.的问题

    报错如下: 300 [main] DEBUG org.apache.hadoop.util.Shell - Failed to detect a valid hadoop home directory ...

  5. Java.Class

    Class类 1. Class继承自Object. 2. .class 和 instance.getClass()的区别 Ref[1] Reference 1. .class http://stack ...

  6. How to Solve Lonsdor K518ISE Abnormal Display by Factory Resetting

    Here’s the working solution to Lonsdor K518ISE Key Programmer abnormal display after upgrade. Proble ...

  7. HDU 6126.Give out candies 最小割

    Give out candies Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. MySQL学习笔记-数据库内存

    数据库内存 InnoDB存储引擎内存由以下几个部分组成:缓冲池(buffer pool).重做日志缓冲池(redo log buffer)以及额外的内存池(additional memory pool ...

  9. Permutation Sequence LT60

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  10. Select isnull(,)

    类似于 select isnull(...,1) from table 这样的 isnull函数防止查询结果为空,防止接下来需要这个值时可能造成空指针异常