POJ - 1094 Sorting It All Out (拓扑排序)

题目链接 http://poj.org/problem?id=1094

题目

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.

思路 分为3种情况,第一种是每个顶点的入度都不为0,这里就会有环路,无法形成拓扑排序。第二种情况是入度为了0的顶点不为1,这样就不能保证拓扑排序唯一性了。第三种情况就是保证入度为0的顶点只有一个,保证了拓扑排序的唯一性。

AC代码

#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int mapn[27][27]; //领接矩阵
int in[50],q[50]; // in表示入度,q存储排列
int toposort(int n)
{
    int s1[50],flog=1,m,w,c; 
    c=0;
    for(int i=0;i<n;i++)
        s1[i]=in[i];
    for(int i=0;i<n;i++){
        m=0;
        for(int j=0;j<n;j++){
            if(s1[j]==0){
                m++;
                w=j;
            }
        }
        if(m==0)
            return 0;
        if(m>1)
            flog=-1;
        q[c++]=w; // 将入度为0的点存储到q
        s1[w]=-1; // 标记此点已用
        for(int k=0;k<n;k++){
            if(mapn[w][k]==1) // 如果w到k有通路
               s1[k]--; // k点的入度减1
        }
    }
    return flog;
}
int main()
{
    int key,n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)
            break;
        memset(mapn,0,sizeof(mapn));
        memset(in,0,sizeof(in));
        char s[5];
        key=0; // 如果已经判断出来,则用key做标记
        for(int i=1;i<=m;i++){
            scanf("%s",s);
            if(key)
                continue;
            int x=s[0]-'A';
            int y=s[2]-'A';
            mapn[x][y]=1;
            in[y]++;
            int w=toposort(n);
            if(w==0){ // 第二种情况
                printf("Inconsistency found after %d relations.\n",i);
                key=1;
            }
            else if(w==1){ // 第三种情况
                printf("Sorted sequence determined after %d relations: ",i);
                for(int j=0;j<n;j++)
                    printf("%c",q[j]+'A');
                printf(".\n");
                key=1;
            }
        }
        if(!key) // 第一种情况
            printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37291934/article/details/89643352