Sorting It All Out

原创 2012年03月28日 00:47:51
Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19627   Accepted: 6711

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

East Central North America 2001

#include<stdio.h>
#define MAX 30
int n,m;
int data[MAX][MAX],sorted[MAX];
int topologic(int left[],int right[],int used[])
{
    int i,j,k,total;
    int count=0,order=1;
    int judge[MAX],samearray[MAX];
    for(i=1;i<MAX;i++)
    {
        judge[i]=right[i];
        if(!used[i])
            judge[i]=-1;
    }
    while(1)
    {
        i=1,total=0;
        while(i<=n)
        {
            while(i<=n&&judge[i]!=0) 
                i++;
            if(i>n)
                break;
            samearray[++total]=i;
            judge[i]=-1;
            sorted[++count]=i;
        }
        if(total==0)
            break;
        if(total>1)
            order=0;
        for(i=1;i<=total;i++)
        {
            j=samearray[i];
            for(k=1;k<=left[j];k++)
                judge[data[j][k]]--;
        }
    }
    if(count<n)
    {
        for(i=1;i<=n;i++)
            if(judge[i]>0)
                return 0;
        return -1;
    }
    if(count==n)
    {
        if(!order)
            return -1;
        return 1;            
    }
}
int main()
{
    int i,j,x,y,flag,result;
    char three[5];
    int left[MAX],right[MAX],used[MAX];
    int alternation[MAX*MAX][2];
    while(scanf("%d %d",&n,&m)&&m&&n)
    {
        result=0;
        for(i=0;i<MAX;i++)
        {
            left[i]=0;
            right[i]=0;
            used[i]=0;
            sorted[i]=0;
        }
        for(i=0;i<m;i++)
        {
            scanf("%s",&three);
            alternation[i][0]=three[0]-'A'+1;
            alternation[i][1]=three[2]-'A'+1;
        }
        for(i=0;i<m;i++)
        {
            x=alternation[i][0];
            y=alternation[i][1];
            data[x][++left[x]]=y;
            right[y]++;
            used[x]=used[y]=1;
            flag=topologic(left,right,used);
            if(flag==1)
            {
                printf("Sorted sequence determined after %d relations: ",i+1);
                for(j=1;j<=n;j++)
                    printf("%c",sorted[j]+'A'-1);
                printf(".\n");
                result=1;
                break;
            }
            if(flag==0)
            {
                printf("Inconsistency found after %d relations.\n",i+1);
                result=1;
                break;
            }
        }
        if(!result)
            printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}



Android ANR问题原因分析

自己处理ANR问题单的一点经验,希望能对大家有帮助。
  • sinat_22657459
  • sinat_22657459
  • 2016年10月10日 19:11
  • 7497

JPush极光推送个人理解

个人代码例子 package com.lchy.xwx.mq.common.Jdpush; import java.util.HashMap; import java.util.Map; impo...
  • z313731418
  • z313731418
  • 2015年05月05日 14:32
  • 10800

mmm 主从复制机制排错过程

一. 主机器发出一个sql,导致所有的从机同步出错的问题解决流程a.)进入从机的mysql控制台b.) 查看复制进程的信息mysql> show slave status\Gc.) 关注以下3个key...
  • remote_roamer
  • remote_roamer
  • 2016年07月19日 14:49
  • 1812

Sorting It All Out poj 1094(判断严格小于关系&判断是否有环)

Sorting It All Out An ascending sorted sequence of distinct values is one in which some form ...
  • codeswarrior
  • codeswarrior
  • 2017年11月24日 13:06
  • 80

POJ 1094-Sorting It All Out(元素大小关系-拓扑排序)

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3263...
  • MIKASA3
  • MIKASA3
  • 2016年07月30日 11:48
  • 396

poj 图相关之1094Sorting It All Out

poj 图相关之1094Sorting It All Out Accepted 144K 0MS 测试数据
  • ziqi9216
  • ziqi9216
  • 2017年06月25日 11:09
  • 103

OJ 1094 Sorting It All Out练习小结

注意三种情况的 优先级:不确定
  • tan_change
  • tan_change
  • 2016年09月23日 12:44
  • 146

(POJ 1094)Sorting It All Out top序列 floyd判环

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33760 Ac...
  • STILLxjy
  • STILLxjy
  • 2016年11月16日 19:10
  • 191

[POJ 1094]Sorting It All Out[拓扑排序]

题目链接:[POJ 1094]Sorting It All Out[拓扑排序] 题意分析: 给出字母表中的前N个大写字母和它们之间的M个比较关系,问:从哪句话起能够确定所有关系?或者从哪句话起出现...
  • CatGlory
  • CatGlory
  • 2016年01月20日 00:53
  • 278

拓扑排序 附POJ 1094 Sorting It All Out 解题报告

拓扑排序是针对有向无环图的概念,通常是用于将一些点进行拓扑排序之后形成一个序列能够形成先后的关系,所以拓扑排序可以用于解决点之间又先后关系的序列。         POJ1094这道题是说给你一些大...
  • geniusluzh
  • geniusluzh
  • 2011年09月01日 01:26
  • 1347
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Sorting It All Out
举报原因:
原因补充:

(最多只允许输入30个字)