Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26876   Accepted: 9271

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

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:这题是一个拓扑排序问题,不过过程比较复杂,要处理的情况也比较多!首先题目条件和要求要很清楚,这样分析问题思路就会清晰!

解题代码:

 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
struct Node{ //与之相连的节点结构体
int to;
struct Node *next;
};
Node *Link[]; //链表保存连接关系
Node *tm_node;
bool vis[];
int count[], tm_count[]; //保存节点入度数据,前者为备份数据,后者为运算数据
int n, m, num;
char deal[];
const int A = 'A';
int result[], pos; //保存结果 int TopSort(){
int i, j, cnt;
bool flag = false;
pos = cnt = ;// cnt为入度为0的节点数,pos为保存结果的下标
j = -; //入度为0的字母
for (i = ; i < n; i ++) //寻找入度为0的位置
if(tm_count[i] == && vis[i]){
cnt ++;
j = i;
}
while (~j){
if(cnt > ) //如果cnt > 1 则表示有多个入度为0的节点,也就是说可能无法排序
flag = true; //不直接return是因为还有可能图中有环,导致数据矛盾
for (tm_node = Link[j]; tm_node != NULL; tm_node = tm_node ->next)
tm_count[tm_node ->to] --;
result[pos ++] = j;
tm_count[j] --;
j = -;
cnt = ;
for (i = ; i < n; i ++)
if(tm_count[i] == && vis[i]){
cnt ++;
j = i;
}
}
if(!flag && pos != num) // 图中有环,数据矛盾
return -;
if(!flag && pos == num) //图中为一个有序序列,是否是最终结果还需进一步判断
return ;
if(flag && pos == num) //图中有多个有序序列,还需进一步判断
return ;
if(flag && pos != num) //图中有环,数据矛盾
return -;
} void init(){
num = ;
memset(vis, , sizeof(vis));
memset(Link, , sizeof(Link));
memset(count, , sizeof(count));
memset(tm_count, , sizeof(tm_count));
} void input(){
int d1, d2;
scanf("%s", deal);
d1 = deal[] - A;
d2 = deal[] - A;
tm_node = new Node;
tm_node ->next = NULL;
tm_node ->to = d2;
count[d2] ++;
if(Link[d1] == NULL)
Link[d1] = tm_node;
else{
tm_node ->next = Link[d1];
Link[d1] = tm_node;
}
if(vis[d1] == ){
num ++;
vis[d1] = true;
}
if(vis[d2] == ){
num ++;
vis[d2] = true;
}
} int main(){
int i, j;
int value;
bool had_ans;
while(~scanf("%d%d", &n, &m) && (n && m)){
init(); //初始化
had_ans = false; //是否已经得出结果
for(i = ; i <= m; i ++){
if(had_ans){ //如果已有结果,则无须处理后续数据
scanf("%s", deal);
continue;
}
input(); //数据处理
for(j = ; j < n; j ++)
tm_count[j] = count[j];
value = TopSort(); //拓扑排序
if (value == -){
printf ("Inconsistency found after %d relations.\n", i);
had_ans = true;
}
else if(value == && num == n){ //数据量以满足要求,且能排序
printf ("Sorted sequence determined after %d relations: ", i);
for (j = ; j < n; j ++)
printf ("%c", result[j] + A);
printf(".\n");
had_ans = true;
}
}
if (value == || (value == && n != num)) // 无法排序
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

poj 1094 / zoj 1060 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 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  3. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  4. poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)

    水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...

  5. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

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

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

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

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

  8. poj 1094 Sorting It All Out(图论)

    http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...

  9. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

随机推荐

  1. Webstorm快捷键整理

    Webstorm快捷键整理 F2/Shift F2  下一个/上一个高亮错误 Ctrl+Shift+BackSpace 回到刚刚编辑的地方 Alt+Insert 新建文件,还有其他功能 Ctrl+D ...

  2. ThinkPHP5.0框架开发--第11章 TP5.0 杂项

    ThinkPHP5.0框架开发--第11章 TP5.0 杂项 第11章 TP5.0 杂项 =============================================== 今日学习 1. ...

  3. ORACLE RAC如何增加节点

    ORACLE RAC系统是一个可以横向进行扩展的系统,当一个RAC系统计算能力不满足客户的需求时候,增加节点能够快速增加整个系统的计算能力,使得客户系统计算能力得到一定的提升,以满足客户不断增长的计算 ...

  4. B树索引与索引优化

    B树索引与索引优化 MySQL的MyISAM.InnoDB引擎默认均使用B+树索引(查询时都显示为“BTREE”),本文讨论两个问题: 为什么MySQL等主流数据库选择B+树的索引结构? 如何基于索引 ...

  5. Kettle的改名由来

    不多说,直接上干货! 当时啊,因为很多开源项目到最后都成了无人管的项目,为了避免这种情况的发生,要尽快为Kettle项目构建一个社区.这就意味着,在随后的几年可能需要回答上千封的电子邮件和论坛帖子.幸 ...

  6. Windows常见软件故障及解决方案

    HM NIS Edit: HM NIS Edit 新建程序向导无效,提示“Please specify the setup lang” 说明 NSIS 安装不对.解决方案有二种: 1. 重装 NSIS ...

  7. 关于lncRNA数据收集

    最近需要自己收集数据库里的核酸序列,于是直接面对一些神文 http://www.360doc.com/content/17/0120/08/30227855_623625901.shtml http: ...

  8. .map(function(item)...)这个是按hashcode自动遍历的,怎么才能按照我想要的顺序遍历呢?

    上图是我前端的遍历代码.我的item上有一个name的字段,分别是营业执照,税务登记证和经营许可证,我怎么设置才能让函数每次遍历的时候按照这个顺序遍历,而不是item自带的顺序呢? .map(func ...

  9. layero和index

    zIndex:layer.zIndex, success:function(index,layero){ //layero 为当前层的DOM对象 var zIndex = layer.index; $ ...

  10. 【Tool】Linux下的Spark安装及使用

    1. 确保自己的电脑安装了JAVA Development Kit JDK, 用来编译Java应用, 如 Apache Ant, Apache Maven, Eclipse. 这里是我们安装Spark ...