zoukankan      html  css  js  c++  java
  • Poj/OpenJudge 1094 Sorting It All Out

    1.链接地址:

    http://poj.org/problem?id=1094

    http://bailian.openjudge.cn/practice/1094

    2.题目:

    Sorting It All Out
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 25547   Accepted: 8861

    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

    3.思路:

    4.代码:

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 
     6 using namespace std;
     7 
     8 int n,m;
     9 int map[30][30];
    10 int reg[100];
    11 int in[30],out[30];
    12 char ans[30];
    13 int stack[30];
    14 void fun()
    15 {
    16      int i,j;
    17      for(i='A',j=1;i<='Z';i++,j++)reg[i]=j;
    18 }
    19 void toposort(char *ans)
    20 {
    21     int i,top=0,u,s=0;
    22     for(i=1;i<=n;i++)
    23      if(in[i]==0)stack[top++]=i;
    24     while(top!=0)
    25     {
    26        u=stack[--top];
    27        ans[s++]=u+64;
    28        for(i=1;i<=n;i++)
    29        {
    30          if(map[u][i])
    31          {
    32             in[i]--;
    33             if(!in[i])stack[top++]=i;
    34          }
    35        }
    36     }
    37     ans[s]=0;
    38 }
    39 int main()
    40 {
    41     int i,j,x,y,k,flag1,flag2,flag;
    42     fun();
    43     char ch[5];
    44     while(1)
    45     {
    46       flag1=flag2=0;
    47       memset(map,0,sizeof(map));
    48       scanf("%d%d",&n,&m);
    49       if(n==0&&m==0)break;
    50       for(i=1;i<=m;i++)
    51       {
    52          flag=1;
    53          scanf("%s",ch);
    54          x=reg[ch[0]];
    55          y=reg[ch[2]];
    56          map[x][y]=1;
    57          if(x==y)flag1=i;
    58          memset(in,0,sizeof(in));
    59          memset(out,0,sizeof(out));
    60          if(!flag1&&!flag2)
    61          for(j=1;j<=n;j++)
    62           for(k=1;k<=n;k++)
    63           {
    64              if(j!=x&&k!=y)map[j][k]=map[j][k]||(map[j][x]&&map[y][k]);
    65              if(j==x&&k!=y)map[j][k]=map[j][k]||map[y][k];
    66              if(j!=x&&k==y)map[j][k]=map[j][k]||map[j][x];
    67              if(map[j][k])
    68              {
    69                out[j]++;
    70                in[k]++;
    71              }
    72           }
    73          j=1;
    74          if(!flag1)
    75          for(j=1;j<=n;j++)
    76          {
    77            if(map[j][j])flag1=i;
    78            if(in[j]+out[j]!=n-1)flag=0;
    79          }
    80          if(flag&&!flag2&&j>n){flag2=i;toposort(ans);}
    81       }
    82       if(flag2)
    83       {
    84          printf("Sorted sequence determined after %d relations: %s.
    ",flag2,ans);
    85          continue;
    86       }
    87       if(flag1)
    88       {
    89          printf("Inconsistency found after %d relations.
    ",flag1);
    90          continue;
    91       }
    92       printf("Sorted sequence cannot be determined.
    ");
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    redis qps监控
    不要对md5file.read()计算md5值
    Kubernetes-基于helm安装部署高可用的Redis及其形态探索(二)
    Kubernetes-基于helm安装部署高可用的Redis及其形态探索
    mongodb replication set 主从切换
    使用packstack安装pike版本的openstack
    redis性能测试方法
    mysql与mariadb性能测试方法
    Mongodb集群形式探究-一主一从一仲裁。
    Python元类
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3548236.html
Copyright © 2011-2022 走看看