首页 > 代码库 > Sorting It All Out

Sorting It All Out

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

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.
分析:优先判断有没有环;
   然后判断能不能全部排好序;
   最后才是条件不全;
   前两个利用拓扑排序,入度为0入队列,同一时间有>1个在队列中,条件不全;
   拓扑完还有入度的为环,不成立,输出;
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <bitset>#include <map>#include <queue>#include <stack>#include <vector>#define rep(i,m,n) for(i=m;i<=n;i++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair<int,int>#define sys system("pause")const int maxn=1e4+10;const int N=5e4+10;const int M=N*10*10;using namespace std;inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}inline void umax(ll &p,ll q){if(p<q)p=q;}inline void umin(ll &p,ll q){if(p>q)p=q;}int n,m,k,t,du[26],dudu[26],ret[27];vi e[26];char op[5];int gao(){    int i,j,ok=1;    ret[0]=0;    queue<int>p;    rep(i,0,n-1){du[i]=dudu[i];if(!du[i])p.push(i);}    while(!p.empty())    {        if(p.size()>1)ok=0;        int q=p.front();        p.pop();        ret[++ret[0]]=q;        for(int i=0;i<e[q].size();i++)        {            int x=e[q][i];            if(--du[x]==0)p.push(x);        }    }    rep(i,0,n-1)if(du[i])return 2;    return ok&&ret[0]==n;}int main(){    int i,j;    while(~scanf("%d%d",&n,&k))    {        bool flag=false;        if(!n&&!k)break;        rep(i,0,n-1)dudu[i]=0,e[i].clear();        rep(i,1,k)        {           scanf("%s",op);           if(op[1]==>)swap(op[0],op[2]);           e[op[0]-A].pb(op[2]-A);           dudu[op[2]-A]++;           if(flag)continue;           int x=gao();           if(x==1)           {               printf("Sorted sequence determined after %d relations: ",i);               rep(j,1,ret[0])printf("%c",ret[j]+A);               puts(".");               flag=true;           }           else if(x==2)printf("Inconsistency found after %d relations.\n",i),flag=true;        }        if(!flag)puts("Sorted sequence cannot be determined.");    }    return 0;}

Sorting It All Out