Poj 1094 Sorting It All Out

来源:互联网 发布:百度预测怎么上传数据 编辑:程序博客网 时间:2024/05/15 06:49
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30086 Accepted: 10400

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 6A<BA<CB<CC<DB<DA<B
3 2A<BB<A26 1A<Z
10 40
C<I
E<G
A<J
F<B
D<E
F<D
C<B
E<H
G<I
D<B
C<H
A<B
J<I
D<G
A<E
C<G
E<B
H<G
C<A
F<J
B<G
D<J
E<J
D<H
C<F
B<J
G<J
B<H
D<A
F<I
A<H
C<E
F<H
A<G
B<I
F<A
H<J
F<G
F<E
C<J0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.Sorted sequence determined after 29 relations: CFDAEBHGJI. 这两天心思有点乱,积极改善!!题目大意逗弄错了 - -。就是判断的输入的所有行里,每次输入是否会有上边的3种情况出现。思路是:  通过每次输入进入fi()函数里判断当前的关系中有多少入度为零。如果当前0度的数目>1则不能判断各顺序如果度数==0,则有环出现
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int du[400],d2[400],q[400],Map[400][400];char s[80];int fi(int n){    int i,j,k,m,pos,flag=1;    int c=0;    for(i=0;i<n;i++)    {        d2[i]=du[i];    }    for(i=0;i<n;i++)    {        m=0;        for(j=0;j<n;j++)            if(d2[j]==0)            {                m++;                pos=j;            }        if(m==0)            return 0;        if(m>1)            flag=-1;        q[c++]=pos;        d2[pos]=-1;<span id="transmark"></span>        for(j=0;j<n;j++)            if(Map[pos][j])                d2[j]--;    }    return flag;}int main(){    int n,m,i,j,k,bj,x,y,z;    ios::sync_with_stdio(false);    while(cin>>n>>m&&n&&m)    {        z=bj=0;        memset(Map,0,sizeof(Map));        memset(du,0,sizeof(du));        for(i=1;i<=m;i++)        {            cin>>s;            if(bj)                continue;            x=s[0]-'A';            y=s[2]-'A';            if(Map[x][y]==0)            {                Map[x][y]=1;                du[y]++;            }            k=fi(n);            if( k==1 )            {                printf("Sorted sequence determined after %d relations: ",i);                for(int j=0;j<n;j++)                    printf("%c",q[j]+'A');                printf(".\n");                bj=1;            }            else if(k==0)            {                printf("Inconsistency found after %d relations.\n",i);                bj=1;            }        }        if(!bj)         printf("Sorted sequence cannot be determined.\n");    }    return 0;}


0 0
原创粉丝点击