POJ1094 拓扑排序

来源:互联网 发布:matlab数据导入 编辑:程序博客网 时间:2024/06/02 01:33

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31001 Accepted: 10766

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 <cstdio>#include <cstring>#include <algorithm>#include <map>#include <queue>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;int InE[30];int G[30][30];char ss[30];int n,m,Ti,S;int Topo(){    int cnt = 0;    int tem[30] = {0},fla = 3;    int i,j,S;    f(i,1,n) tem[i] = InE[i];    f(i,1,n){        int sum = 0;        f(j,1,n)            if(!tem[j]){sum++;S = j;}        if(sum == 0) return 1;        if(sum >1)  fla = 2;        ss[cnt++] = 'A' + S - 1;        tem[S] = -1;        f(j,1,n)            if(G[S][j]) tem[j]--;    }    ss[n] = '\0';    return fla;}int main(){    while(~scanf("%d%d",&n,&m)&&(n||m)){        getchar();        char a,b;        int i,j,flag = 0,ok = 0;        memset(G,0,sizeof(G));        memset(InE,0,sizeof(InE));        f(i,1,m){            scanf("%c<%c",&a,&b);            getchar();            if(ok) continue;            G[a-'A'+1][b-'A'+1] = 1;            InE[b-'A'+1]++;            flag = Topo();            if(1 == flag){                printf("Inconsistency found after %d relations.\n",i);                ok = 1;            }            else if(3 == flag){                printf("Sorted sequence determined after %d relations: %s.\n",i,ss);                ok = 1;            }        }        if(!ok)            printf("Sorted sequence cannot be determined.\n");    }    return 0;}
0 0
原创粉丝点击