POJ 1094 Sorting It All Out【拓扑排序】

Sorting It All Out

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 37590 Accepted: 13273

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表示26个字母的前n个,有m个关系,判断这n个字母能否确定唯一的关系,若能输出Sorted sequence determined after i relations: 关系序列. (i代表第i个关系处判断出的结果)若不能,判断是成环的话输出Inconsistency found after i relations. 若不能判断关系输出Sorted sequence cannot be determined.

解题分析:

这道题是一个拓扑排序,每输入一个关系,对相应点的入度进行更新,并进行拓扑排序,直到最后判断出关系。

AC代码:

#include<stdio.h>
#include<string.h>

#define N 110

int e[N][N], du[N], d[N], re[N];

int topo(char *s, int n)
{
    int a = s[0]-'A', b = s[2]-'A';
    int i, j, t = n, flag = 1, coun, top = 0;
    if(!e[a][b]){
        e[a][b] = 1;
        d[b]++;
    }
    for(i = 0; i < n; i++) du[i] = d[i];
    while(t--){
        coun = 0;
        for(i = 0; i < n; i++)
            if(!du[i]){coun++; j = i;}
        if(!coun) return -1;
        else if(coun > 1) flag = 0;//这里只能标记,不能直接return,否则就可能漏掉成环的条件。
        du[j]--;
        re[top++] = j;
        for(i = 0; i < n; i++)
            if(e[j][i]) du[i]--;
    }
    if(flag) return 1;
    return 0;
}

int main()
{
    char s[N];
    int i, j, k, n, m, OK;
    while(scanf("%d%d", &n, &m), n || m){
        if(m < n-1){
            while(m--) scanf("%s", s);
            printf("Sorted sequence cannot be determined.\n");
            continue;
        }
        memset(e, 0, sizeof(e));
        memset(d, 0, sizeof(d));
        for(i = OK = 1; i <= m; i++){
            scanf("%s", s);
            if(OK){
                int a = s[0]-'A', b = s[2]-'A';
                k = topo(s, n);
                if(k == -1){
                    OK = 0;
                    printf("Inconsistency found after %d relations.\n", i);
                }
                else if(k){
                    printf("Sorted sequence determined after %d relations: ", i);
                    for(j = 0; j < n; j++) printf("%c", re[j]+'A');
                    OK = 0;
                    printf(".\n");
                }
            }
        }
        if(OK) printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/k_young1997/article/details/79952467