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. 使用Unison同步服务器目录

    一.Unison简介Unison是Windows.Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致.Unison拥有与其它一些同步工具或文件系 ...

  2. 第十一章 Function类型

    在ECMAScript中,Function(函数)类型实际上是对象.每个函数也是Function类型的实例,而且都与其它引用类型一样具有属性和方法.由于是函数对象,因此函数名实际上也是一个指向函数对象 ...

  3. Pitfalls of the Hibernate Second-Level / Query Caches--reference

    This post will go through how to setup the Hibernate Second-Level and Query caches, how they work an ...

  4. sql时间段算法

    需求:领导要求写时间段算法,格式如下 :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: :-: 一小时制 :-: :-: :-: ...

  5. 在Objective-C声明Block的几种方式

    1.作为局部变量 returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; 2.作为类的属性 @property ...

  6. Android_scrollview

    xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  7. Linux学习之路:shell变量(一)

    一.变量的显示与设置 1.变量的显示运用echo命令 +$符号: 上图例子显示的是系统变量,咱们可以自己设置变量 2.设置变量运用“=”符号 设置了变量NIU 值为“niunai” 变量设置规则: ( ...

  8. sysctl

    /proc/sys目录下存放着大多数内核参数,并且可以在系统运行时进行更改,不过重新启动机器就会失效./etc/sysctl.conf是一个允许改变正在运行中的Linux系统的接口,它包含一些TCP/ ...

  9. 使用DS18B20设计温控系统

    datasheet真的是得看看啊,比如DS18B20,不然程序都不好写,美国DALLAS半导体公司推出的数字化温度传感器DS18B20采用单总线协议,即与单片机接口仅需要一个IO口,无需任何外部原件, ...

  10. 使用jvisualvm和飞行记录器分析Java程序cpu占用率过高

    一.jvisualvm使用 JDK1.6中Oracle提供了一个新的JVM监控工具:jvisualvm.下面重点介绍如何在本地通过远程的方式打开Linux服务器上的jvisualvm. 1.Xmana ...