zoukankan      html  css  js  c++  java
  • poj1094 topsort

    Sorting It All Out
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 32275   Accepted: 11210

    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.

    题目大意:给你字母表的前N个字母以及M个他们之间的关系,问最终是全序,偏序,还是出现矛盾。

    思路分析:有向图,很明显拓扑排序,这是我做的第二道拓扑排序的题目,在学习图论之前一直以为拓扑

    排序就是单纯的一种排序方式,现在才知道它是图论的一部分,有向图,因为本题不仅要求输出结果,还要判断

    在输入第几个顺序时得出的结果,因此每输入一组数据就要进行一拓扑排序,拓扑排序本质上一种偏序排序,

    当然在一定情况下会出现全序的特殊情况,j!=n,就说明有向图的构建过程中出现了环,有可能直接是反向环,

    也有可能是绕一圈后回来,如果队列中元素>1,说明队列中存在两个以上优先级相同的元素,即有向图是偏序的。

    不说了,上代码,本题对于拓扑排序的理解还是十分有用的。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=27;
    int indegree[maxn],ans[maxn],graph[maxn][maxn];
    int n,m;
    int topsort()
    {
        int flag=0;
        int in[maxn];
        int j=0;
        memcpy(in,indegree,sizeof(indegree));//一定要重新定义一个数组,防止对主函数中indegree数组产生影响
        queue<int> q;
        for(int i=0;i<n;i++)
        {
            if(in[i]==0) q.push(i);//将入度为0的点放入队列
        }
        while(!q.empty())
        {
            if(q.size()>1)  flag=1;
            int t=q.front();
            q.pop();//记得弹出
            ans[j++]=t;
            for(int i=0;i<n;i++)
            {
                if(graph[t][i])
                {
                    in[i]--;
                    if(in[i]==0) q.push(i);
                }
            }
        }
        if(j!=n)//不能拓扑排序 ,存在环
            return 1;
        else if(flag==1) return 2;
        else return 0;
    }
    int main()
    {
        char s[5];
        while(scanf("%d%d",&n,&m)&&(n||m))
        {
            int incon=0,detmin=0;
            memset(graph,0,sizeof(graph));
            memset(indegree,0,sizeof(indegree));
            for(int i=1;i<=m;i++)
            {
                scanf("%s",s);
                if(detmin||incon) continue;
                int a=s[0]-'A';
                int b=s[2]-'A';
                if(graph[a][b]==0)
                {
                    graph[a][b]=1;
                    indegree[b]++;
                }
                int res=topsort();
                if(res==0)
                {
                    detmin=1;
                    printf("Sorted sequence determined after %d relations: ",i);
                    for(int i=0;i<n;i++)
                    printf("%c",ans[i]+'A');
                    printf(". ");
                }
                if(res==1)
                {
                    incon=1;
                    printf("Inconsistency found after %d relations. ",i);
                }
            }
            if(!incon&&!detmin) printf("Sorted sequence cannot be determined. ");
        }

    }

  • 相关阅读:
    (IOCP)-C#高性能Socket服务器的实现
    GraphQL和RESTful的区别
    HTTP Client Performance Improvements
    foobar2000 iOS使用,并连接PC的歌曲进行播放
    Spring中基于AOP的@AspectJ
    Spring中基于AOP的XML架构
    Spring框架的AOP
    Spring的AOP AspectJ切入点语法详解(转)
    Spring中实现自定义事件
    Spring的事件处理
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5581919.html
Copyright © 2011-2022 走看看