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;
}



相关文章推荐

ZOJ-1060 Sorting It All Out

Sorting It All Out Time Limit: 2 Seconds      Memory Limit: 65536 KB An ascending sorted seque...

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

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

poj 1094 Sorting It All Out

题目链接:http://poj.org/problem?id=1094

poj-1094 Sorting It All Out[拓扑排序]

感觉需要注意的细节有点多。 (1)先判断是否有环  用used表示能拓扑的点 vis表示能搜索到的点 假如两者总数不等说明有环。 (2)再判断拓扑序是否唯一 假如一次寻找0入度节点有多个即不唯...

POJ - 1094 Sorting It All Out解题报告

Kahn····

POJ 1094 Sorting It All Out (拓扑排序)

题目链接 #include #include #include using namespace std; struct node { int in;//入度 bool valid...
  • szhhck
  • szhhck
  • 2012年08月12日 12:40
  • 450

POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

Description An ascending sorted sequence of distinct values is one in which some form of a less-than...

POJ1094 Sorting It All Out

题目来源:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory L...

JOJ 1089 & ZOJ 1060 & poj 1094 Sorting It All Out (邻接表的栈拓扑排序模板)

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is...
  • jxy859
  • jxy859
  • 2011年07月08日 20:38
  • 376

poj1094Sorting It All Out(拓扑排序)

这题挺奇怪的,我之前还真么做过这样的拓扑排序,之前卡了一阵子,这个题目是判断到第几个成环,到第几个正好能唯一排序,另一种就是怎么也排不了序,就是每个数据跑一遍拓扑排序就行了#include #inc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Sorting It All Out
举报原因:
原因补充:

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