Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29539   Accepted: 10233

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

网上讲解参考代码

/**  这道题WA了好久,其中有几个需要注意的地方
1、当出现正好存在一种情况能够排序完所有节点时,不管以后的边会出现什么情况,都输出
    能够排序成功
2、当中间的拓扑排序过程中出现多个几点的入度为0时,只记录当时的状态
    (亦即该测试数据要么出现环,要么就是有多组解),不能立即返回,
    要继续读边,直到能够排序完成(此时输出有多解的情况)或者出现环。
*/
#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;

bool G[30][30];
int d[30];
char s[30];
int n;
int toposort()
{
    int num,k,i,j,t;
    int td[30];
    bool flag1=true;
    for(i=1;i<=n;++i)
        td[i]=d[i];
    memset(s,0,sizeof(0));
    for(j=0;j<n;++j)
    {
        num=0;
        for(i=1;i<=n;++i)
        {
            if(td[i]==0)
            {
                k=i;
                ++num;
            }
        }
        if(num==0)    //有环
            return -1;
        if(num>1)  //有多种情况,还需继续读边判断
        {
            flag1=false;
        }
        s[j]='A'+k-1;
        td[k]--;
        for(t=1;t<=n;++t)
        {
            if(G[k][t])
                --td[t];
        }
    }
    s[n]='\0';
    if(flag1==false)  //情况不唯一
        return 0;
    return 1;      //全部排好序了返回1.
}

int main()
{
    int m,i,ans,k;
    bool flag;
    char temp[5];
    while(scanf("%d%d",&n,&m),m||n)
    {
        memset(G,false,sizeof(G));
        memset(d,0,sizeof(d));
        flag=true;
        for(i=1;i<=m;++i)
        {
            scanf("%s",temp);
            if(!flag)         //已经排好序或者有环
                continue;
            int u=temp[0]-'A'+1;
            int v=temp[2]-'A'+1;
            if(!G[u][v])
            {
                ++d[v];
                G[u][v]=true;
            }
            ans=toposort();
            if(ans==1||ans==-1)
            {
                k=i;
                flag=false;
            }
        }
        if(ans==1)
        {
            printf("Sorted sequence determined after %d relations: %s.\n",k,s);
        }
        else if(ans==-1)
        {
            printf("Inconsistency found after %d relations.\n", k);
        }
       else if(flag)
            printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

我的代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int map[100][100];
int m,n;
int tindegree[100],indegree[100];
char str[5];
char s[39];
int toposort(){
     bool flag=true;
     memset(tindegree,0,sizeof(tindegree));
     memset(s,0,sizeof(s));
     for(int i=1;i<=n;i++){
     tindegree[i]=indegree[i];
     }
    for(int i=1;i<=n;i++){
        int sum=0,k;
           for(int j=1;j<=n;j++){
               if(!tindegree[j]){
                    k=j;
                    sum++;
               }
           }
           if(sum==0){
              return -1;
           }
           if(sum>1){
                flag=false;
           }
        s[i-1]=k+'A'-1;
        tindegree[k]--;
        for(int z=1;z<=n;z++){
            if(map[k][z]){
                 tindegree[z]--;
            }
        }

}
    s[n]='\0';
    if(flag==false)
    return 0;
    return 1;
}
int main(){
     while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0)
           break;
          memset(map,0,sizeof(map));
          memset(indegree,0,sizeof(indegree));
          memset(str,0,sizeof(str));
          memset(s,0,sizeof(s));
          int ans=2;
          int temp;
           bool flag=true;
          for(int i=1;i<=m;i++){
              scanf("%s",str);
              if(flag==false)
              continue;
              int u=str[0]-'A'+1;
              int v=str[2]-'A'+1;
              if(!map[u][v]){
                 map[u][v]=1;
                 indegree[v]++;
              }
             ans=toposort();
             if(ans==-1||ans==1){
                 temp=i;
                 flag=false;
             }
          }
          if(ans==1)
          printf("Sorted sequence determined after %d relations: %s.\n",temp,s);//temp不可以改为n
          else  if(ans==-1)
          printf("Inconsistency found after %d relations.\n",temp);
          else  if(flag)
          printf("Sorted sequence cannot be determined.\n");

}
    return 0;
}

最新文章

  1. weui dialog
  2. javax/javaee-api/ Maven依赖
  3. grails的layouts模板页面使用
  4. iOS字符串拆分
  5. spring html特殊字符操作
  6. 过程式编程 drawShapes
  7. My97datepicker设置后一个日期大于前一个日期
  8. 队列(链式存储)C++模板实现
  9. haproxy nginx 多路径
  10. setObject与setValue的区别
  11. easyui小清新俺也晒晒 视频管理软件bs项目
  12. live555—VS2010/VS2013 下live555编译、使用及测试(转载)
  13. 数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份
  14. 高并发数据库之MySql性能优化
  15. apache软件包下载地址
  16. simhash类的使用
  17. ExtJS学习(四)EditorGrid可编辑表格
  18. margin-right没有效果的问题
  19. C# 属性(Property)和字段(Field)的区别
  20. 背水一战 Windows 10 (81) - 全球化

热门文章

  1. linux中的服务
  2. EntityFramework_MVC4中EF5 新手入门教程之一 ---1.创建实体框架数据模型
  3. 20步打造最安全的NGINX WEB服务器
  4. knockout_主页的demo复习
  5. RHCS
  6. BZOJ2120 数颜色
  7. css常用技巧
  8. 类,抽象基类,接口类三者间的区别与联系(C++)
  9. 千万不要误用 java 中的 HashCode 方法
  10. hdu 2041 超级楼梯