• POJ1094 Sorting It All Out LUOGU 排序


       
     
                  Sorting It All Out
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 40012   Accepted: 14072

    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.






    P1347 排序

    题目描述

    一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D。在这道题中,我们将给你一系列形如A<B的关系,并要求你判断是否能够根据这些关系确定这个数列的顺序。

    输入输出格式

    输入格式:

    第一行有两个整数n,m,n表示需要排序的元素数量,2<=n<=26,第1到n个元素将用大写的A,B,C,D....表示。m表示将给出的形如A<B的关系的数量。

    接下来有m行,每行有3个字符,分别为一个大写字母,一个<符号,一个大写字母,表示两个元素之间的关系。

    输出格式:

    若根据前x个关系即可确定这n个元素的顺序yyy..y(如ABC),输出

    Sorted sequence determined after xxx relations: yyy...y.

    若根据前x个关系即发现存在矛盾(如A<B,B<C,C<A),输出

    Inconsistency found after 2 relations.

    若根据这m个关系无法确定这n个元素的顺序,输出

    Sorted sequence cannot be determined.

    (提示:确定n个元素的顺序后即可结束程序,可以不用考虑确定顺序之后出现矛盾的情况)

    输入输出样例

    输入样例#1: 
    1:
    4 6
    A<B
    A<C
    B<C
    C<D
    B<D
    A<B
    
    2:
    3 2
    A<B
    B<A
    
    3:
    26 1
    A<Z
    输出样例#1: 
    1:
    Sorted sequence determined after 4 relations: ABCD.
    2:
    Inconsistency found after 2 relations.
    3:
    Sorted sequence cannot be determined.

    由题目描述可知:

      该题 典型的拓扑排序。

      1、在 拓扑排序时,当出现同时可访问两个节点或多个节点时,则信息不完全。

      2、当出现有环时,则有冲突,判断是否有环可以在拓扑排序完时判断是否有点未访问。因为若无环,每个点都可以访问。

    不多说了,附上我的AC代码:

     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 const int MAXN=2500;
     7 int n,m,cas,id;
     8 
     9 struct edge
    10 {
    11     int v,nx;
    12 }set[MAXN];
    13 int head[30],d[30],ok[30],write[30];
    14 queue<int> Q;
    15 
    16 void Addedge(int u,int v)
    17 {
    18     id++;set[id].v=v;set[id].nx=head[u];
    19     head[u]=id;
    20 }
    21 
    22 int bfs()
    23 {
    24     cas=0;
    25     while(!Q.empty())Q.pop();
    26     int out=1,t,u;t=0;
    27     for(int i=1;i<=n;i++)
    28         if(!ok[i])
    29         {
    30             t++;Q.push(i);
    31         }
    32     if(t>1)out=0;
    33     while(!Q.empty())
    34     {
    35         u=Q.front();Q.pop();t=0;write[++cas]=u;
    36         for(int k=head[u];k>0;k=set[k].nx)
    37         {
    38             ok[set[k].v]--;
    39             if(!ok[set[k].v])
    40             {
    41                 t++;Q.push(set[k].v);
    42             }
    43         }
    44         if(t>1)out=0;
    45     }
    46     for(int i=1;i<=n;i++)if(ok[i]>0){out=-1;break;}
    47     return out;
    48 }
    49 
    50 char print()
    51 {
    52     for(int i=1;i<=cas;i++)printf("%c",(char)(write[i]+64));
    53     return '.';    
    54 }
    55 
    56 int main()
    57 {
    58     while(~scanf("%d%d",&n,&m))
    59     {
    60         memset(write,0,sizeof(write));    
    61         memset(d,0,sizeof(d));    
    62         memset(head,-1,sizeof(head));
    63         id=0;
    64         int now=0,loca;
    65         char a,b,c;
    66         if(n==0 && m==0)break;
    67         for(int i=1;i<=m;i++)
    68         {
    69             cin>>a>>b>>c;
    70             if(b=='<')
    71             {
    72                 Addedge(a-64,c-64);d[c-64]++;
    73             }
    74             else 
    75             {
    76                 Addedge(c-64,a-64);d[a-64]++;
    77             }
    78             if(now==0)
    79             {
    80                 for(int j=1;j<=n;j++)ok[j]=d[j];
    81                 now=bfs();loca=i;
    82             }
    83         }
    84         if(now==0)puts("Sorted sequence cannot be determined.");
    85         else if(now==1)printf("Sorted sequence determined after %d relations: ",loca),printf("%c
    ",print());
    86         else printf("Inconsistency found after %d relations.
    ",loca);
    87     }
    88     return 0;
    89 }

    记得点赞欧。

    P1347 排序

  • 相关阅读:
    eclipse fail to create java virtual machine
    sas软件连接Oracle数据库的办法
    JAVA中数据的读取与写入,不同类型数据的转换
    二维数组的在函数中的传递
    【转】值传递与引用传递
    【转】深拷贝与浅拷贝
    (转)JS报表控件highcharts应用
    highstocks.js使用指南
    (转)Highcharts使用指南(出处:http://liuhaorain.cnblogs.com )
    jQuery实现checkbox全选,反选
  • 原文地址:https://www.cnblogs.com/lzxzy-blog/p/10328346.html
Copyright © 2020-2023  润新知