1.链接地址:

http://poj.org/problem?id=1094

http://bailian.openjudge.cn/practice/1094

2.题目:

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25547   Accepted: 8861

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

3.思路:

4.代码:

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib> using namespace std; int n,m;
int map[][];
int reg[];
int in[],out[];
char ans[];
int stack[];
void fun()
{
int i,j;
for(i='A',j=;i<='Z';i++,j++)reg[i]=j;
}
void toposort(char *ans)
{
int i,top=,u,s=;
for(i=;i<=n;i++)
if(in[i]==)stack[top++]=i;
while(top!=)
{
u=stack[--top];
ans[s++]=u+;
for(i=;i<=n;i++)
{
if(map[u][i])
{
in[i]--;
if(!in[i])stack[top++]=i;
}
}
}
ans[s]=;
}
int main()
{
int i,j,x,y,k,flag1,flag2,flag;
fun();
char ch[];
while()
{
flag1=flag2=;
memset(map,,sizeof(map));
scanf("%d%d",&n,&m);
if(n==&&m==)break;
for(i=;i<=m;i++)
{
flag=;
scanf("%s",ch);
x=reg[ch[]];
y=reg[ch[]];
map[x][y]=;
if(x==y)flag1=i;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
if(!flag1&&!flag2)
for(j=;j<=n;j++)
for(k=;k<=n;k++)
{
if(j!=x&&k!=y)map[j][k]=map[j][k]||(map[j][x]&&map[y][k]);
if(j==x&&k!=y)map[j][k]=map[j][k]||map[y][k];
if(j!=x&&k==y)map[j][k]=map[j][k]||map[j][x];
if(map[j][k])
{
out[j]++;
in[k]++;
}
}
j=;
if(!flag1)
for(j=;j<=n;j++)
{
if(map[j][j])flag1=i;
if(in[j]+out[j]!=n-)flag=;
}
if(flag&&!flag2&&j>n){flag2=i;toposort(ans);}
}
if(flag2)
{
printf("Sorted sequence determined after %d relations: %s.\n",flag2,ans);
continue;
}
if(flag1)
{
printf("Inconsistency found after %d relations.\n",flag1);
continue;
}
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

Poj/OpenJudge 1094 Sorting It All Out的更多相关文章

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

    http://poj.org/problem?id=1094 原来拓扑序可以这样做,原来一直sb的用白书上说的dfs............ 拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即 ...

  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. poj 2388 insert sorting

    /** \brief poj 2388 insert sorting 2015 6 12 * * \param * \param * \return * */ #include <iostrea ...

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

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

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

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

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

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

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

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

  9. poj.1094.Sorting It All Out(topo)

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

随机推荐

  1. SQLite使用教程10 运算符

    SQLite 运算符 SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算. 运算符用于指定 SQLite 语句中的条件 ...

  2. windows8 认识及使用

    windows8的一次技术分享. 利用国庆宅家的几天,在跑不动XP的老笔记本上装了win8,嘿,跑的溜溜的,一高兴做个ppt给公司的同事们介绍介绍,随意之作,勿较真抬杠,呵呵. 文件地址:http:/ ...

  3. [Effective C++ --005]了解C++默默编写并调用哪些函数

    <前言>编译器是个十分敬业的工作者,不但为你编译代码,甚至为你生成代码,不可思议吧.本文主要介绍编译器究竟会为我们生成和调用哪些代码. <空类和非空类>如果问什么样的类是空类? ...

  4. 《RESTful Web Services》第二章 识别资源

    引言:开放RESTful Web服务的首要步骤之一是设计资源模型. 2.1 如何从领域名词中识别资源   2.2 如何选择资源粒度     一些因素会影响数据库表和对象模型的设计,例如领域建模.需要高 ...

  5. ExtJs 下拉菜单分页工具插件 代码分析

    Ext.ns("Ext.ux"); //创建插件对象 Ext.ux.PageSizePlugin = function(){ //调用父对象的构造方法,并为此插件生成一个预定义st ...

  6. 自定义请求头信息及cookie信息

    请求网页的时候有时候我们需要传递一些参数信息,这个时候我们可以将参数放到请求头中,具体使用如下: 这里有小问题就是请求参数的属性名好像不支持下划线,即 HTTP_UID不可用,但是HTTP-UID就可 ...

  7. nginx配置文件特殊字符说明

    开发过程中经常重复配置nginx.conf,对里面的特殊字符始终不太明白具体的意义,今天百度nginx配置看到一篇不错的文章,转载记录下来,以备不时之需. nginx rewrite 正则表达式匹配 ...

  8. [转]oracle odp.net 32位/64位版本的问题

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/04/19/2020793.html 如果你的机器上安装了odp.net,且确信machine.conf ...

  9. saltstack实战2--远程执行之返回(returner)

    saltstack有3大功能:远程执行,配置管理,云管理 其中远程执行又可分解为:目标,模块,返回  这3个部分. 比如下面语句 [root@master ~]# salt '*' test.ping ...

  10. 电脑小白学习软件开发-C#语言基础之循环重点讲解,习题

    写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用视频. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是不在话下. 本教程是基础教程,适合任何有志于学习软件开发的 ...