poj1094 Sorting It All Out (拓扑排序)

来源:互联网 发布:爱知世博会logo涵义 编辑:程序博客网 时间:2024/04/30 01:54
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 32462Accepted: 11290

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<B3 2A<BB<A26 1A<Z0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
这道题还是很多坑,都discuss里面提到的,其实这种题目做起来如果一开始不知道很浪费时间的。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define cls(x,y) memset((x),(y),sizeof((x)));const int maxn=35;int n,m;int deg[maxn];int copydeg[maxn];int head[maxn];struct note {    int to,next;} a[maxn*100];bool used[maxn];int res[maxn];int top;queue<int> que;vector<string> vec;void ADD(int u,int v) {    a[top].next=head[u];    a[top].to=v;    head[u]=top++;    ++deg[v];}int topSORT() {    while(!que.empty()) {        que.pop();    }    int fl=0,tsnum=0;    for(int i=0; i<n; ++i) {        if(!deg[i])            que.push(i),++fl,res[tsnum++]=i;    }    if(fl>=2)fl=1;    else fl=0;    while(!que.empty()) {        int t=que.front();        que.pop();        int pu=0;        for(int i=head[t]; ~i; i=a[i].next) {            int to=a[i].to;            --deg[to];            if(!deg[to])que.push(to),++pu,res[tsnum++]=to;;        }        if(pu>1)fl=1;    }    if(tsnum<n)return -1;    if(fl)return -2;    return tsnum;}int main() {    #ifdef tangge    freopen("1094.txt","r",stdin);    #endif // tangge    while(~scanf("%d%d",&n,&m)&&(n+m)) {        top=0;        vec.clear();        cls(head,-1)        cls(deg,0)        cls(used,false)        int ans=-1;        for(int i=0; i<m; ++i) {            char in[10];            cls(in,0)            scanf("%s",in);            if(~ans)continue;//            cout<<"scanf="<<in<<endl;//            string str;//            str.clear();//            str.assign(in);////////            if(vec.size()&&find(vec.begin(),vec.end(),str)!=vec.end()){////                cout<<vec.size()<<endl;//                continue;//            }////            cout<<"input="<<in<<endl;//            vec.push_back(str);            if(in[1]=='<') {                ADD(in[0]-'A',in[2]-'A');            } else {                ADD(in[2]-'A',in[0]-'A');            }            memcpy(copydeg,deg,sizeof(deg));            int fl=topSORT();            if(fl>0) {                ans=1;                printf("Sorted sequence determined after %d relations: ",i+1);                for(int j=0; j<fl; ++j)putchar('A'+res[j]);                puts(".");            } else if(fl==-1) {                ans=1;                printf("Inconsistency found after %d relations.\n",i+1);            }            memcpy(deg,copydeg,sizeof(deg));        }        if(ans==-1)printf("Sorted sequence cannot be determined.\n");    }    return 0;}
0 0
原创粉丝点击