poj 1094 Sorting It All Out (拓扑排序)

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20350   Accepted: 6999

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.

第一道拓扑排序的题
思路很简单,每次取入度为0的点,然后删掉与这个点相连的边,也就是和该点相连的点的入度都减一
因为这道题要判断矛盾、可排序和不能排序几种情况
因此要判断队列中的点的数量来决定
如果只有一个点,正常情况
两个以上就出现无法判定
如果队列为空就是出现矛盾
#include<iostream>
#include<queue>
using namespace std;
int n;
int map[30][30];
int mark[30];
int innum[30];
char input[5];

int unknown, confilict, sorted, printed;
int res[30];
int resnum;

int topsort()
{
    int temp[30];
    queue<int> q;
    memset(res,0,sizeof(res));
    resnum=0;
    unknown=0;

    for(int i=0;i<n;i++)
    {
        temp[i]=innum[i];
        if(temp[i]==0)
        q.push(i);    
    }
    while(q.empty()==0)
    {
        if(q.size()>1)
            unknown=1;
        int k=q.front();
        q.pop();
        res[resnum++]=k;
        for(int i=0;i<n;i++)
        {
            if(map[k][i]==1)
            {
                temp[i]--;
                if(temp[i]==0)
                    q.push(i);
            }
        }
    }
    if(resnum<n)
        return 1;
    if(unknown==1)
        return 2;
    return 3;
}

int main()
{
    int m;
    int in;
    int r;
//    freopen("e:\\data.txt", "r", stdin);
//    freopen("e:\\out.txt", "w", stdout);
    while(1)
    {
        cin>>n>>m;
        if(n==0 && m==0)
            break;
        resnum=0;
        unknown=0;
        
        memset(map, 0, sizeof(map));
        memset(res, 0, sizeof(res));
        memset(innum, 0, sizeof(innum));
        r=2;
        for(in=0;in<m;in++)
        {
            cin>>input;
            if(map[input[0]-'A'][input[2]-'A']!=1)
                innum[input[2]-'A']++;
            map[input[0]-'A'][input[2]-'A']=1;
            r=topsort();
            if(r!=2)
                break;
        }
        if(r==3)
        {
            cout<<"Sorted sequence determined after "<<in+1<<" relations: ";
            for(int i=0;i<resnum;i++)
                cout<<char(res[i]+'A');
            cout<<"."<<endl;
        }
        else if(r==1)
        {
            cout<<"Inconsistency found after "<<in+1<<" relations."<<endl;
        }
        else if(r==2)
        {
            cout<<"Sorted sequence cannot be determined."<<endl;
        }
        for(in++;in<m;in++)
        {
            cin>>input;
        }
    }
}

 

posted @ 2012-06-18 22:31  w0w0  阅读(174)  评论(0)    收藏  举报