Sorting It All Out poj1094 (拓扑排序的变形 自己感觉对拓扑理解很有意义)


Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33961   Accepted: 11884

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 
  

Sample Output

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

就是问你在第几个关系表达式(操作)之后能保证唯一确定的拓扑序列

!如果存在则输出

操作数以及唯一拓扑序列

!!如果不存在则输出

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

2.在第几步出现环

Accode :

#include 
#include 
#include 
#include 
#include 

using namespace std;

int indegree[33]; ///入度
bool Map[33][33]; ///临接矩阵
char out[33]; ///拓扑排序输出数组

int Top(int n)
{
    priority_queue , greater > q;
    /// 优先队列/队列/栈此题都可 为了统一自己拓扑就用优先队列吧
    int i,k;
    int zeroflag=0; ///1表示有多个入度0点
    int num=0,temp[33]; ///临时入度数组
    memcpy(temp,indegree,sizeof(indegree)); ///indegree复制过来

    for(i=0; i1)
        {
            zeroflag=1;
        }
        ///要注意在while里面判断 因为此过程中也可能出现入度0不止一个的情况
        k=q.top();
        q.pop();
        num++;
        out[num]=k+'A'; ///num计数 以存到out里面
        for(i=0; i>n>>m && n||m)
    {
        circleflag=0;
        orderflag=0;
        memset(Map,0,sizeof(Map));
        memset(indegree,0,sizeof(indegree));

        for(i=1; i<=m; i++)
        {
            cin>>s;
            if(circleflag == 0 && orderflag == 0) ///一旦有了结果 就不读图了
            {
                if(Map[s[2]-'A'][s[0]-'A'] == 1)
                {
                    circleflag=1; ///表示有环了
                    cout<<"Inconsistency found after "<


你可能感兴趣的:(poj初级,1094,拓扑排序,Sorting,It,All,Out,各种排序)