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

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.

Source

解题思路:

拓扑排序的应用。參考http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html做的。

本题须要注意的问题非常多,有点“坑”。以下是从上面博文中转的,()里面的内容是我自己加的。

题意:给你一些大写字母间的偏序关系,然后让你推断是否能唯一确定它们之间的关系,或者所给关系是矛盾的,或者到最后也不能确定它们之间的关系。

分析:

用拓扑排序:

1.拓扑排序能够用栈来实现,每次入栈的是入度为0的节点(也能够用队列,或者不使用队列和栈,循环n次,找入度为0的点)。

1.拓扑排序的结果一般分为三种情况:1、能够推断(拓扑排序有唯一的结果) 2、有环出现了矛盾(出现了没有入度为0的节点) 3、条件不足,不能推断.

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

网上对于这道题的错误提示(须要注意):

1.本题顺序:

a.先判有没有环,有环就直接输出不能确定;

b.假设没有环,那么就看会不会有多种情况,假设有多种情况就再读下一行;假设所有行读完还是有多种情况,就是确定不了;

c.假设最后没有环,也不存在多种情况(即每次取出来入度为零的点仅仅有一个),那么才是答案;

2.有答案就先出答案,无论后面的会不会矛盾什么的;

3.假设在没有读全然部输入就能出答案,一定要把剩下的行都读完。

代码:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stack>
#include <queue>
using namespace std;
int indegree[30];//保存入度
int graph[30][30];//是否有边
char output[30];//输出可确定序列
bool ok;//能够被确定
bool dilemma;//有环,矛盾
bool no;//不能被确定
char c1,c,c2;//输入 int topo(int n)
{
int in[30];
for(int i=0;i<n;i++)
in[i]=indegree[i];//使用备用数组进行拓扑排序 stack<int>s;//入度为0的点进栈
for(int i=0;i<n;i++)
if(!in[i])
s.push(i); bool flag=0;//栈里面入度为0的元素大于一个时,不确定的拓扑排序
int cnt=0;//入度为0的元素个数,也是输出序列里面的个数
while(!s.empty())
{
if((s.size())>1)
flag=1;//不确定
int first=s.top();
s.pop();
output[cnt++]=first+'A';//放入输出序列里面
for(int i=0;i<n;i++)
if(graph[first][i])//与入度为0的元素相连的元素
{
in[i]--;
if(in[i]==0)
s.push(i);//入栈
}
}
if(cnt!=n)//假设没有环的话,序列里面的元素个数肯定等于输入的元素个数,就算在某个元素未输入之前,它的入度也初始化为0
return 2;//有环
else if(flag==1)//不确定的拓扑排序
return -1;
return 1;
} int main()
{
int n,m;
while(cin>>n>>m&&(n||m))
{
ok=0;dilemma=0;no=0;
memset(indegree,0,sizeof(indegree));
memset(graph,0,sizeof(graph));
for(int i=1;i<=m;i++)
{
cin>>c1>>c>>c2;//当出现矛盾或者通过一些条件可被确定序列,剩下的输入条件就不须要再处理了
if(!ok&&!dilemma)//没有环,没有确定的拓扑排序,这里的拓扑排序必须输入的字母都有。比方输入ABCD 那么仅仅有AB不是确定的
{
int t1=c1-'A';
int t2=c2-'A';
if(graph[t2][t1])//双向边,有环,出现矛盾
{
cout<<"Inconsistency found after "<<i<<" relations."<<endl;
dilemma=1;//出现矛盾
continue;
} if(!graph[t1][t2])
{
graph[t1][t2]=1;
indegree[t2]++;//入度++
}
int ans=topo(n);//确定返回1,有环返回2
if(ans==2)
{
cout<<"Inconsistency found after "<<i<<" relations."<<endl;
dilemma=1;
continue;
}
if(ans==1)
{
cout<<"Sorted sequence determined after "<<i<<" relations: ";
for(int k=0;k<n;k++)
cout<<output[k];
cout<<"."<<endl;
ok=1;
}
}
}
if(!ok&&!dilemma)
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}

[ACM] POJ 1094 Sorting It All Out (拓扑排序)的更多相关文章

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

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

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

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

  3. [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 ...

  4. 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 ...

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

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

  6. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

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

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

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

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

  9. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

随机推荐

  1. amazeui学习笔记--css(常用组件13)--进度条Progress

    amazeui学习笔记--css(常用组件13)--进度条Progress 一.总结 1.进度条基本使用:进度条组件,.am-progress 为容器,.am-progress-bar 为进度显示信息 ...

  2. Spring Boot中的缓存支持(一)注解配置与EhCache使用

    Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...

  3. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  4. JAVA初始开发环境搭建

    上午想在一台新电脑上搭建java开发环境,在没有之前备份的情况下,单靠网络还真有点麻烦.最主要的原因是貌似在我当前的网络环境下jdk无法下载,官网这个链接半天打不开,http://www.oracle ...

  5. JAVA 日志库3

        Commons Logging和SLF4J都是基于相同的设计,即从一个LogFactory中取得一个命名的Log(Logger)实例,然后使用这个Log(Logger)实例打印debug.in ...

  6. embed-it_Integrator memory compile工具使用之一

    embed-it_Integrator memory compile工具使用之一 主要内容 使用Integrator compile memory 使用Integrator 对比筛选适合的memory ...

  7. 大数据(十四) - Storm

    storm是一个分布式实时计算引擎 storm/Jstorm的安装.配置.启动差点儿一模一样 storm是twitter开源的 storm的特点 storm支持热部署,即时上限或下线app 能够在st ...

  8. 2015第30周四Java日志组件

    Java 日志 API 从功能上来说,日志 API 本身所需求的功能非常简单,只需要能够记录一段文本即可.API 的使用者在需要进行记录时,根据当前的上下文信息构造出相应的文本信息,调用 API 完成 ...

  9. 网站访问优化(二):开启apache服务器gzip压缩

    昨天,把带宽从1M升级到2M,使用cdn版本的jquery之后,网站访问速度由平均5s(在禁止缓存的情况下,使用缓存大概在2.8s)下降到2.8s的样子. 今天,继续优化. 第1步:   把图片进行了 ...

  10. 用IBM WebSphere DataStage进行数据整合: 第 1 部分 分类: H2_ORACLE 2013-08-23 11:20 688人阅读 评论(0) 收藏

    转自:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0602zhoudp/ 引言 传统的数据整合方式需要大量的手工 ...