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. 7 种流行 PHP IDE 的比较

    编写关于 PHP 的系列文章让我更加深刻地了解了 PHP 开发人员的世界.我和许多 PHP 程序员交谈过,最令我惊奇的是只有很少的人使用 IDE.大多数程序员使用文本编辑器,比如 Microsoft® ...

  2. UvaLive 6667 Longest Chain (分治求三元组LIS&amp;树状数组)

    题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...

  3. uboot官方FTP下载地址

    ftp://ftp.denx.de/pub/u-boot/

  4. send,recv,sendto,recvfrom

    send函数 int send( SOCKET s,    const char FAR *buf,    int len,    int flags ); 不论是客户还是server应用程序都用se ...

  5. Android 带进度的圆形进度条

    最近项目有个需求,做带进度从下到上的圆形进度条. 网上查了一下资料,发现这篇博客写得不错http://blog.csdn.net/xiaanming/article/details/10298163 ...

  6. Reviewing the Blog Module

    Reviewing the Blog Module Throughout the tutorial, we have created a fully functional CRUD module us ...

  7. GPS(Global Positioning System)全球定位系统

    GPS构成: 1.空间部分 GPS的空间部分是由24 颗工作卫星组成,它位于距地表20 200km的上空,均匀分布在6 个轨道面上(每个轨道面4 颗) ,轨道倾角为55°.此外,还有4 颗有源备份卫星 ...

  8. Nodepad ++

    Nodepad ++是一个很好用的记事本工具.轻巧.打开文本速度要比windows内置的记事本快,而且功能众多. 支持多种语言,关键字高亮显示 并且可以折叠 内置n种主题,随时换 文件内容一变随时提醒 ...

  9. 【shell】 I/O重定向

    1.基本概念  a.I/O重定向通常与 FD有关,shell的FD通常为10个,即 0-9:  b.常用FD有3个,为0(stdin,标准输入).1(stdout,标准输出).2(stderr,标准错 ...

  10. #284 div.2 C.Crazy Town

    C. Crazy Town   Crazy Town is a plane on which there are n infinite line roads. Each road is defined ...