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

原创 2018年04月15日 20:01:27

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;
}
版权声明:本文为博主原创文章,转载请注明本文链接,并在下边告诉一声哦。 https://blog.csdn.net/k_young1997/article/details/79952467

POJ1094-Sorting It All Out

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299234850   提示:拓扑排序 这道题有隐含这一信息,每输入一对关系,如果判...
  • lyy289065406
  • lyy289065406
  • 2011-07-30 09:25:16
  • 7290

POJ-1094 Sorting It All Out 解题报告

Description An ascending sorted sequence of distinct values is one in which some form of a less-t...
  • angle555945
  • angle555945
  • 2012-03-24 01:03:05
  • 910

NYOJ_349 Sorting It All Out 拓扑排序

描述 An ascending sorted sequence of distinct values is one in which some form of a less-than operato...
  • S031302306
  • S031302306
  • 2016-07-10 15:01:17
  • 143

POJ1094 字母排序(拓扑排序)

对于N个大写字母,给定它们的一些偏序关系,要求判断出经过多少个偏序关系之后可以确定它们的排序或者存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序。 利用拓扑排序即可解决这个问题,但由于题目...
  • alongela
  • alongela
  • 2012-12-05 16:28:14
  • 3628

[poj1094]拓扑排序经典例题

Sorting It All OutTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 34710 Acce...
  • youhavepeople
  • youhavepeople
  • 2017-03-28 14:41:44
  • 1864

poj 1094 Sorting It All Out(拓扑排序)

http://poj.org/problem?id=1094 拓扑排序,有点坑。。 给出m个关系,在输入的同时判断在当前条件下是否有环,是否能形成唯一拓扑序列,或者是不确定。 此题要注意优先级,当在...
  • u013081425
  • u013081425
  • 2014-03-26 00:23:25
  • 1609

POJ 1094 Sorting It All Out(拓扑排序)

题意:n个字母排序,给定m个关系,输出 ①在得到几个关系后确定排序(全序) ②在得到几个关系后出现矛盾,有环 ③不能得到确定的排序(偏序) 思路:直白的拓扑排序,但很多要注意的地方 1)每读...
  • qq_24833289
  • qq_24833289
  • 2015-03-14 20:53:24
  • 117

POJ 1094 Sorting It All Out 拓扑排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than...
  • AXuan_K
  • AXuan_K
  • 2014-11-04 16:50:32
  • 510

POJ 1094 Sorting It All Out 拓扑排序

题目描述:DescriptionAn ascending sorted sequence of distinct values is one in which some form of a less-...
  • wuxuanyi27
  • wuxuanyi27
  • 2016-06-18 11:19:58
  • 169

POJ 1094:Sorting It All Out (拓扑排序)

Sorting It All OutTime limit:1000 ms Memory limit:65768 kB Problem DescriptionAn ascending sorted s...
  • xp731574722
  • xp731574722
  • 2017-09-16 12:01:34
  • 104
收藏助手
不良信息举报
您举报文章:POJ 1094 Sorting It All Out【拓扑排序】
举报原因:
原因补充:

(最多只允许输入30个字)