sdutacm-Sorting It All Out

来源:互联网 发布:inapp软件 编辑:程序博客网 时间:2024/06/03 03:33

 

Sorting It All Out

Time Limit: 1000MS MemoryLimit: 10000KB

SubmitStatistic

ProblemDescription

Anascending sorted sequence of distinct values is one in which some form of aless-than operator is used to order the elements from smallest to largest. Forexample, 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

Inputconsists of multiple problem instances. Each instance starts with a linecontaining two positive integers n and m. the first value indicated the numberof objects to sort, where 2 <= n <= 26. The objects to be sorted will bethe first n characters of the uppercase alphabet. The second value m indicatesthe number of relations of the form A < B which will be given in thisproblem instance. Next will be m lines, each containing one such relationconsisting of three characters: an uppercase letter, the character"<" and a second uppercase letter. No letter will be outside therange of the first n letters of the alphabet. Values of n = m = 0 indicate endof input.

Output

Foreach problem instance, output consists of one line. This line should be one ofthe 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 sortedsequence is determined or an inconsistency is found, whichever comes first, andyyy...y is the sorted, ascending sequence.

ExampleInput

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

ExampleOutput

Sorted sequence determinedafter 4 relations: ABCD.

Inconsistency found after 2relations.

Sorted sequence cannot bedetermined.

Hint

poj1094有链接提示的题目请先去链接处提交程序,AC后提交到SDUTOJ中,以便查询存档。

Author

East Central North America 2001

#include <iostream>#include<bits/stdc++.h>using namespace std;const int oo=105;int n,m,in[oo],temp[oo],s,t,pos,num,Sort[oo];vector<int>g[oo];queue<int>q;void init(){    memset(in,0,sizeof(in));    for(int i=0;i<=n;i++)    g[i].clear();}inline bool find(int u,int v){    for(int i=0;i<g[u].size();++i)    {        if(g[u][i]==v)return true;    }    return false;}int toposort(){    while(!q.empty())q.pop();    for(int i=0;i<n;i++)    {        if(in[i]==0)        {        q.push(i);        }    }    pos = 0;    bool unsure = false;    while(!q.empty())    {        if(q.size()>1)unsure = true;//假如存在多个起点说明拓扑排序不唯一        int t = q.front();        q.pop();        Sort[pos++] = t;        for(int i=0;i<g[t].size();i++)        {            if(--in[g[t][i]]==0)            {                q.push(g[t][i]);            }        }    }    if(pos<n) return 1;//扫描点个数小于N说明存在环路    if(unsure) return 2;//继续寻找    return 3; //发现唯一拓扑排序}int main(){    int x,y,i,flag,ok,stop;    while(~scanf("%d%d%*c",&n,&m))    {        if(!n||!m)        {            break;        }        init();        flag = 2;        ok  = false;        char X,O,Y;        for(i=1;i<=m;i++)        {            scanf("%c%c%c%*c",&X,&O,&Y);            if(ok)continue;//当非(不确定拓扑序列)即单一序列或者出现环时,只输入不处理            x = X-'A',y = Y-'A';            if(!find(y,x))//当不存在此路径时            {            g[y].push_back(x);            ++in[x];            }            else if(!find(x,y))            {            g[x].push_back(y);            ++in[y];            }            memcpy(temp,in,sizeof(in)); // 拷贝一个副本,等下用来还原in数组            flag = toposort();            memcpy(in,temp,sizeof(temp));            if(flag!=2)            {                stop = i;                ok = true;            }         }    if(flag ==3)    {    printf("Sorted sequence determined after %d relations: ",stop);    for(i = pos-1;i>=0;--i)    {        printf("%c",Sort[i]+'A');    }    printf(".\n");    }    else if(flag==1)    {    printf("Inconsistency found after %d relations.\n",stop);    }    else    {        printf("Sorted sequence cannot be determined.\n");    }}return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 0msTake Memory: 164KBSubmit time: 2017-02-20 16:35:15****************************************************/

0 0
原创粉丝点击