poj 1094 Sorting It All Out

来源:互联网 发布:sqlserver 教程 编辑:程序博客网 时间:2024/05/16 11:19
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 23567 Accepted: 8145

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.
拓扑排序,输出“矛盾优先”于输出“不确定”,如果所有关系都输入完,如存在矛盾和不确定,则输出矛盾。
进行拓扑排序时,有三种情况:
1.若得到的拓扑序列长度小于已出现的字母数量,则可以判定为矛盾,不管全部关系是否确定,都直接输出矛盾;
2.若在已出现的字母中入度为0的结点多于1个,则标记为不确定。(标记certain=false);
3.若得到的拓扑序列长度等于最大范围(即n个)字母数量,同时关系为确定的(certain=true),则输出拓扑序列。
若全部关系输入完毕,但没有出现1和3的情况,则输出不确定。
AC代码:
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <cmath>#include <vector>#include <cstdlib>using namespace std;vector<int>v[30];int in1[30],in2[30];int topo[30];bool app[30];bool toposort(int n,int m,int num){    queue<int>q;    bool vis[30];    memset(vis,false,sizeof(vis));    for(int i=0;i<n;i++)    if(in2[i]==0&&app[i])    //入度为0且已出现的字母    q.push(i);    int k=0;    bool certain=true;    while(!q.empty())    {        if(q.size()>1)         //入度为0的结点多于一个        certain=false;        int x=q.front();        q.pop();        topo[k++]=x;        vis[x]=true;        for(int i=0;i<v[x].size();i++)        {            in2[v[x][i]]--;            if(in2[v[x][i]]==0&&!vis[v[x][i]])            q.push(v[x][i]);        }    }    if(k<num)         //拓扑序列长度小于已出现的字母数量    {        printf("Inconsistency found after %d relations.\n",m);        return true;    }    if(k==n&&certain)   //拓扑序列长度等于最大范围(即n个)字母数量,同时关系为确定的(certain=true)    {    printf("Sorted sequence determined after %d relations: ",m);    for(int i=0;i<n;i++)    printf("%c",topo[i]+'A');    printf(".\n");    return true;    }    return false;}int main(){     int n,m;     char s[5];     while(scanf("%d%d",&n,&m)!=EOF)     {         if(!n&&!m) break;         for(int i=0;i<30;i++)         v[i].clear();         bool flag=false;         memset(in1,0,sizeof(in1));         memset(app,false,sizeof(app));         int sum=0;         for(int i=1;i<=m;i++)         {             scanf("%s",s);             int a=s[0]-'A';             int b=s[2]-'A';             if(!app[a])             {                 app[a]=true;                 sum++;             }             if(!app[b])             {                 app[b]=true;                 sum++;             }             v[a].push_back(b);             in1[b]++;             for(int j=0;j<30;j++)             in2[j]=in1[j];             if(!flag)             flag=toposort(n,i,sum);         }         if(!flag)   //若全部关系输入完毕,但没有出现1和3的情况,则输出不确定            printf("Sorted sequence cannot be determined.\n");     }     return 0;}


原创粉丝点击