[poj1094]拓扑排序经典例题

Sorting It All Out

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34710 Accepted: 12170
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

对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序,样例数据已经说得够详细了。
1.如果存在则输出

    操作数以及唯一拓扑序列

2.如果不存在则输出

    1.拓扑序列不唯一确定(很显然 如果存在不唯一入度为0的点就出现这种状况)
    2.在第几步出现环

因为前提是要先判断几个关系式后出现上述情况,所以每输入一次都要拓扑一次

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 50;
int in[N]; //入度
bool Map[N][N]; //临接矩阵此题可以随便用 
char out[N]; //拓扑排序输出数组

int topsort( int n ) {
    priority_queue<int,vector<int>,greater<int> > q;  //优先队列忠实粉丝 
    int flag = 0,k;
    int zeroflag = 0;
    int num = 0,temp[N];
    memcpy(temp,in,sizeof(in));//拷贝
    for( int i = 0; i < n; i++ ) {
        if( !in[i] ) q.push(i);
    }
    while( !q.empty() ) {
        if( q.size() > 1 )
            zeroflag = 1;//要注意在while里面判断 因为此过程中也可能出现入度0不止一个的情况
        k = q.top();
        q.pop();
        num++;
        out[num] = k + 'A'; //num计数 以存到out里面
        for(int i = 0; i < n; i++ )
            if(Map[k][i]==1 && --temp[i] == 0)
                q.push(i);
    }
    if( num != n ) return 1;
    if( zeroflag == 1 ) return 0;//多个点入度为零
    return -1; 
} 
int main(){
    int n,m,k;
    int step; //记录操作数
    int circleflag,orderflag;
    char s[6];
    while( scanf("%d%d", &n, &m) != EOF && n ){
        circleflag=0;orderflag=0;
        memset(Map,0,sizeof(Map));
        memset(in,0,sizeof(in));
        for( int i = 1; i <= m; i++ ){
            scanf("%s", s);
            if( circleflag == 0 && orderflag == 0 ){ //已经判出了 就不读了
                if(Map[s[2]-'A'][s[0]-'A'] == 1){
                    circleflag=1; //有环了
                    printf("Inconsistency found after %d relations.\n", i);
                    continue ;
                }
                if(Map[s[0]-'A'][s[2]-'A'] == 0){
                    Map[s[0]-'A'][s[2]-'A'] = 1;
                    in[s[2]-'A']++;
                }
                k = topsort(n);
                if( k == 1 ){
                    circleflag=1;
                    printf("Inconsistency found after %d relations.\n", i);
                }
                else if( k == -1 ){
                    orderflag=1;
                    step=i; //记录位置
                }
            }
        }
        if(circleflag == 0 && orderflag == 0)
            printf("Sorted sequence cannot be determined.\n");
        else if(orderflag == 1){
            printf("Sorted sequence determined after %d relations: ", step);
            for(int i=1; i<=n; i++)
                printf("%c", out[i]);
            printf(".\n");
        }
    }
    return 0;
}
    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/youhavepeople/article/details/67636690
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞