zoukankan      html  css  js  c++  java
  • poj 1094 Sorting It All Out 拓补排序

    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.

    题意:给一些大写字母的偏序关系。判断是否存在唯一的关系,或者他们的关系是矛盾的,即造成了回路,或者到最后不能确定他们的关系。

    用拓扑排序:

    1.拓扑排序可以用栈来实现,每次入栈的是入度为0的节点。

    1.拓扑排序的结果一般分为三种情况:1、可以判断 2、有环出现了矛盾 3、条件不足,不能判断.

    2.这道题不仅需要判断这三种情况,而且还要判断在处理第几个关系时出现前两种情况,对于本道题来说三种情况是有优先级的。前两种情况是平等的谁先出现先输出谁的相应结果,对于第三种情况是在前两种情况下都没有的前提下输出相应结果的.

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <stack>
     4 #include <cstring>
     5 using namespace std;
     6 const int maxn=30;
     7 int n,m;
     8 int pb[maxn][maxn],rd[maxn],list[maxn];
     9 int toposort()
    10 {
    11     int i,j=0;
    12     int in[maxn];
    13     memcpy(in,rd,sizeof(rd)); //复制入度数组,以防改变原数组
    14     stack <int> s;
    15     for(i=0; i<n; i++) //入度为0的入栈
    16     {
    17         if(!in[i])
    18             s.push(i);
    19     }
    20     bool flag=false;
    21     while(!s.empty())
    22     {
    23         if(s.size()>1)
    24             flag=true;
    25         int t=s.top(); // 记录出栈数字
    26         s.pop();
    27         list[j++]=t;
    28         for(i=0; i<n; i++)
    29         {
    30             if(pb[t][i])
    31             {
    32                 in[i]--; //入度减一
    33                 if(!in[i])
    34                     s.push(i); //入度为0的继续入栈 
    35             }
    36         }
    37     }
    38     if(j!=n) //存在回路
    39         return 1;
    40     else if(flag) //有多重排序方式
    41         return 2;
    42     return 0;
    43 }
    44 int main()
    45 {
    46     while(cin>>n>>m,n||m)
    47     {
    48         memset(pb,0,sizeof(pb));
    49         memset(rd,0,sizeof(rd));
    50         char a,b;
    51         int frist,scend;
    52         frist=scend=0;
    53         for(int i=1; i<=m; i++)
    54         {
    55             getchar();
    56             scanf("%c<%c",&a,&b);
    57             if(!frist && !scend)
    58             {
    59                 if(pb[b-'A'][a-'A']==1) //存在反向边
    60                 {
    61                     scend=1;
    62                     printf("Inconsistency found after %d relations.
    ",i);
    63                     continue;
    64                 }
    65                 if(!pb[a-'A'][b-'A'])
    66                 {
    67                     pb[a-'A'][b-'A']=1;
    68                     rd[b-'A']++;
    69                 }
    70                 int res=toposort();  //进行拓扑排序
    71                 if(res==0) //序列能够唯一被确定
    72                 {
    73                     printf("Sorted sequence determined after %d relations: ",i);
    74                     for(int j=0; j<n; j++)
    75                         printf("%c",list[j]+'A');
    76                     printf(".
    ");
    77                     frist=1;
    78                 }
    79                 else if(res==1) //存在回路
    80                 {
    81                     scend=1;
    82                     printf("Inconsistency found after %d relations.
    ",i);
    83                 }
    84             }
    85 
    86         }
    87         if(!frist&&!scend) //即不能唯一约定,又不构成回路
    88             printf("Sorted sequence cannot be determined.
    ");
    89     }
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    mysql添加用户
    ubantu更新源
    内存的crash记录分析
    windows下sublime2 clojure环境配置
    服务器使用recast navigation
    网络库crash以及boost asio strand dispath分析
    CentOS7 监控进程网络流量工具安装
    vs2013提高编译速度
    Centos6.5 gitlab安装使用
    linux命令行将已有项目提交到github
  • 原文地址:https://www.cnblogs.com/cxbky/p/4851227.html
Copyright © 2011-2022 走看看