Sorting it ALL Out(POJ_1094)

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 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

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

Source

East Central North America 2001

代码

Floyd+拓扑排序(目前是从别人那抄的

#include <iostream>
#include <cstring>

using namespace std;

int map[50][50];
int n,m,flag;
int ans[50];

void Floyd(int n)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                if(!map[j][k]&&map[j][i]&&map[i][k])
                    map[j][k]=1;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        if(map[i][i])
            flag=1;
    }
}

int Toposort(int n)
{
    int outdegree[50];
    int vis[50];
    memset(outdegree,0,sizeof(outdegree));
    memset(vis,0,sizeof(vis));
    memset(ans,0,sizeof(ans));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(map[i][j])
                outdegree[j]++;
        }
    }
    int cnt,pos=0;
    for(int i=0;i<n;i++)
    {
        cnt=0;
        for(int j=0;j<n;j++)
        {
            if(outdegree[j]==0&&!vis[j])
            {
                cnt++;
                pos=j;
            }
        }
        if(cnt>1) return 0;
        else if(cnt==0)
        {
            flag=1;
            return 1;
        }
        ans[i]=pos;
        vis[pos]=1;
        for(int j=0;j<n;j++)
        {
            if(map[pos][j])
                outdegree[j]--;
        }
    }
    flag=2;
    return 2;
}

int main()
{
    while(cin>>n>>m,n||m)
    {
        memset(map,0,sizeof(map));
        char a[10000],b,c[10000];
        int i;
        flag=0;
        for(i=0; i<m; i++)
        {
            cin>>a[i]>>b>>c[i];
        }
        for(i=0;i<m;i++)
        {
            map[a[i]-'A'][c[i]-'A']=1;
            Floyd(n);
            if(flag) break;
            if(Toposort(n)) break;
        }
        if(i==m) cout<<"Sorted sequence cannot be determined."<<endl;
        else if(flag==1) cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
        else if(flag==2)
        {
            cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
            for(i=0; i<n; i++)
            {
                cout<<(char)(ans[i]+'A');
            }
            cout<<'.'<<endl;
        }
    }
    return 0;
}

你可能感兴趣的:(Sorting it ALL Out(POJ_1094))