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. Android application framework 分析[in process]

    application activity application service application UI system application sdk tool JVM 1 activity t ...

  2. Zorka监控平台的Online reconfiguration基本效果展示

    在上一篇日志中,我简介了Zorka的Online reconfiguration的用法,可是没怎么介绍如何看到在线更改的效果,这里简单说说. 还是以之前的tomcat为例,我们在文件夹zorka\sc ...

  3. [HEOI2016/TJOI2016] 排序 解题报告(二分答案/线段树分裂合并+set)

    题目链接: https://www.luogu.org/problemnew/show/P2824 题目描述: 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在 ...

  4. express+模板引擎构建项目时遇到的几个小问题

    1.启动项目/调试项目 项目启动用:npm start 由于每次更改路由代码后必须重启服务才可以看效果,所以为了达到热加载的效果我们安装 supervisor:全局安装也可以: npm install ...

  5. MySQL内置函数uuid和uuid_short

    MySQL的uuid这个函数.简要介绍一下. 用法 简单看到,这个值,每次执行都是不同的. 生成规则 第1 2 3 段是与时间有关的. time_low.time_mid.time_high_and_ ...

  6. 学习es6 setter/getter研究

    1.背景 在ES6中,我们对类的定义如下 class Person { // 构造函数 constructor (name) { // 属性初始化 this.name = name; } // 成员方 ...

  7. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  8. DotNetCore.1.0.1-VS2015Tools.Preview2.0.2 安装错误分析及解决办法(so far)

    折腾了这么多天总算弄完了,真恶心.为了让其他童靴避免掉进我遇到的坑里,我决定把最近遇到问题及其解决办法总结一下,希望对大家有帮助. 1.对于2016年7月底以前安装VS用户来说,可能不会那么迫切安装这 ...

  9. 修改input标签中的placeholder样式

    input::-webkit-input-placeholder { color: #fff !important; } input:-moz-placeholder { color: #fff !i ...

  10. C++调用约定和名字约定 thiscall

    调用约定: __cdecl __fastcall与 __stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数 ...