• POJ1094[有向环 拓扑排序]


    Sorting It All Out
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 33184   Accepted: 11545

    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


    因为要求几个relation,所以没加一个进行一次topoSort
    一旦遇到有环或者唯一有序就sign=1,后面的关系一定要读完(后果自负),如果最后sign==0说明没确定顺序
    要打印方案,可以用char  topo[]来保存,然而输出topo[]比一个个输出慢了16MS,并且还忘初始化了WA好几次
    //
    //  main.cpp
    //  poj1094
    //
    //  Created by Candy on 9/11/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int N=30;
    int n,m,ind[N],ch[N][N];
    char tmp[5],topo[N];
    int q[N];
    int topoSort(){
        int flag=2;//only sorted
        int indt[N];
        for(int i=1;i<=n;i++) indt[i]=ind[i];
        for(int i=1;i<=n;i++){//printf("%d %d %d 
    ",i,ind[i],ch[i][0]);
            int zero=0,u=0;
            for(int j=1;j<=n;j++) if(indt[j]==0) zero++,u=j;
            if(zero==0) return 1;//circle
            if(zero>1) flag=3;//no sorted
            indt[u]--;
            topo[i-1]=(char)u+'A'-1;
            q[i-1]=u;
            for(int j=1;j<=ch[u][0];j++)
                indt[ch[u][j]]--;
        }
        return flag;
    }
    int main(int argc, const char * argv[]) {
        while(cin>>n>>m){
            if(n==0&&m==0) break;
            memset(ch,0,sizeof(ch));
            memset(ind,0,sizeof(ind));
            memset(topo,0,sizeof(topo));
            int sign=0;
            
            for(int i=1;i<=m;i++){
                scanf("%s",tmp);
                if(sign) continue;
                int x=tmp[0]-'A'+1,y=tmp[2]-'A'+1;
                ind[y]++;
                ch[x][++ch[x][0]]=y;
                int flag=topoSort();
                if(flag==1){sign=1;printf("Inconsistency found after %d relations.
    ",i);}
                if(flag==2){
                    sign=1;printf("Sorted sequence determined after %d relations: %s.
    ",i,topo);
    //                printf("Sorted sequence determined after %d relations: ",i);
    //                for(int j=0;j<n;j++)
    //                    printf("%c",q[j]+'A'-1);
    //                printf(".
    ");
    //                sign=1;
                }
            }
            if(!sign) printf("Sorted sequence cannot be determined.
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    The Road to Ryu: Hi Ryu
    Python学习札记(三十五) 面向对象编程 Object Oriented Program 6
    Python学习札记(三十四) 面向对象编程 Object Oriented Program 5
    Python学习札记(三十三) 面向对象编程 Object Oriented Program 4
    Python学习札记(三十二) 面向对象编程 Object Oriented Program 3
    Coursera SDN M1.2.1 SDN History: Programmable Networks 2
    Python学习札记(三十一) 面向对象编程 Object Oriented Program 2
    Python学习札记(三十) 面向对象编程 Object Oriented Program 1
    Coursera SDN M1.2.1 SDN History: Programmable Networks 1
    Python学习札记(二十九) 模块2
  • 原文地址:https://www.cnblogs.com/candy99/p/5863329.html
Copyright © 2020-2023  润新知