拓扑排序---(poj 1094)

本文介绍了一种基于拓扑排序的算法实现,该算法用于确定一组元素是否能够根据给定的元素间关系形成一个升序排列。通过具体的样例输入输出展示了如何检测排序的可行性、发现不一致情况以及判断无法确定的情况。
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36338 Accepted: 12786

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.


提示:一个基于拓扑排序的算法,就是输出输出和输入不好弄;



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

int topsort( int n ) {//传入参数为节点数;


    stack<int> q;
    int zeroflag = 0;
    int num = 0,temp[N];
    memcpy(temp,in,sizeof(in));//拷贝,为了将原来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不止一个的情况;
        int k = q.top();
        q.pop();
        num++;
        out[num] = k + 'A'; //num计数 以存到out里面
        for(int i = 0; i < n; i++ )
            if(Map[k][i]==true && --temp[i] == 0)
                q.push(i);
    }


    if( num != n ) return 1;//表示有环
    if( zeroflag == 1 ) return 0;//多个点入度为零
    return -1;//表示无环
}

int main(){
    int n,m;

    char s[6];
    while(~scanf("%d%d", &n, &m) &&n&&m){


        int step = 0; //记录操作数
        int 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'] == true){//有关系就为true;
                    circleflag=1; //有环了
                    printf("Inconsistency found after %d relations.\n", i);
                    continue ;
                }

                if(Map[s[0]-'A'][s[2]-'A'] == false){//读入第一组数据;
                    Map[s[0]-'A'][s[2]-'A'] = true;
                    in[s[2]-'A']++;//入读;
                }
                int 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值