zoukankan      html  css  js  c++  java
  • Sorting It All Out 拓扑排序+确定点

    这一道题的话  数据有一点问题    ........     例如 不过 还是   能理解一下  试试吧  .........

    3 5
    A<B
    B<C
    C<A
    A<C
    B<A
    这几组数据 明显反映出来  这是成环的    ,      然后  按照 输入输出案例来说 这个是 有序的   ABC 

      

         

    题目要求     在每组数据的   第一行  给你需要排序 的 字母数    和  他们之间的关系数量      然后  输入每组数据    你首先许亚萍判断在输入  第几组 数据的时候 出现了 环     其次判断    到第几组关系的时候   可以确定唯一的序列  如果上面两个 都不行的话    就输出   第三种情况  不能确定  唯一 的   排序序列

    内存越界.....醉了 . 明天看  睡觉觉 

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 #include<set>
     9 #include<stack>
    10 #include<string>
    11 #include<sstream>
    12 #include<map>
    13 #include<cctype>
    14 using namespace std;
    15 int n,m,a[30][30],visited[30],flag,fuck,mark,result[30],temp[30],count1,flag1;
    16 void topsort(int q)
    17 {
    18     fuck=0;
    19     for(int j=0;j<n;j++)
    20         temp[j]=visited[j];   //
    21     count1=0;
    22     for(int i=1;i<=n;i++)         //   其实只是普通的  拓扑排序   重复化了一下而已   ...
    23     {
    24         fuck=mark=0;
    25         for(int j=0;j<n;j++)
    26         {
    27             if(temp[j]==0)
    28             {
    29                 flag=j;
    30                 mark++;
    31             }
    32         }
    33         if(mark==0)
    34         {
    35             printf("Inconsistency found after %d relations.
    ",q+1);
    36             flag1=fuck=1;
    37             break;
    38         }
    39         temp[flag]--;  //  找到了   flag  他没有儿子  / 现在  将他标记为 -1;
    40         if(mark==1)
    41         {
    42             result[i]=flag;  //  将  该点储存起来
    43             count1++;
    44         }
    45         for(int j=0;j<n;j++)   //  将 flag的 所有爸爸的 儿子数  -1
    46         {
    47             if(a[flag][j])
    48             {
    49                 temp[j]--;
    50             }
    51         }
    52     }
    53 }
    54 int main()
    55 {
    56     while(scanf("%d%d",&n,&m),(n||m))
    57     {
    58         flag1=fuck=count1=mark=flag=0;
    59         memset(visited,0,sizeof(visited));
    60         memset(a,0,sizeof(a));
    61         for(int i=0;i<m;i++)
    62         {
    63             char d,c,b;
    64             scanf(" %c%c%c",&b,&d,&c);
    65             if(flag1)
    66                 continue;
    67             a[b-'A'][c-'A']=1;   //     c 有一个叫做b 的儿子
    68             visited[c-'A']++;        //  c 的 儿子 数量  ++
    69             topsort(i);          //  第一次进去的时候 就相当于   只有一组的关系
    70             if(fuck)
    71                 ;
    72             else
    73             {
    74                 if(count1==n)
    75                 {
    76                     printf("Sorted sequence determined after %d relations: ",i+1);
    77                     for(int i=1;i<=n;i++)
    78                         printf("%c",result[i]+'A');
    79                     printf(".
    ");
    80                     flag1=1;
    81                 }
    82             }
    83             if(!flag1&&i==m-1)
    84             {
    85                 printf("Sorted sequence cannot be determined.
    ");
    86                 flag1=1;
    87             }
    88         }
    89     }
    90     return 0;
    91 }
     1 #include<stdio.h>
     2 #include<string.h>
     3 int map[27][27],indegree[27],q[27];
     4 int TopoSort(int n) //拓扑排序
     5 {
     6     int c=0,temp[27],loc,m,flag=1,i,j;  ////flag=1:有序 flag=-1:不确定
     7     for(i=1;i<=n;i++)
     8         temp[i]=indegree[i];
     9     for(i=1;i<=n;i++)
    10     {
    11         m=0;
    12         for(j=1;j<=n;j++)
    13             if(temp[j]==0) { m++; loc=j; }  //查找入度为零的顶点个数
    14         if(m==0) return 0;  //有环
    15         if(m>1) flag=-1;  // 无序
    16         q[c++]=loc;   //入度为零的点入队
    17         temp[loc]=-1;
    18         for(j=1;j<=n;j++)
    19             if(map[loc][j]==1) temp[j]--;
    20     }
    21     return flag;
    22 }
    23 
    24 int main()
    25 {
    26     int m,n,i,sign;  //当sign=1时,已得出结果
    27     char str[5];
    28     while(scanf("%d%d",&n,&m))
    29     {
    30         if(m==0&&n==0) break;
    31         memset(map,0,sizeof(map));
    32         memset(indegree,0,sizeof(indegree));
    33         sign=0;
    34         for(i=1;i<=m;i++)
    35         {
    36             scanf("%s",str);
    37             if(sign) continue; //一旦得出结果,对后续的输入不做处理
    38             int x=str[0]-'A'+1;
    39             int y=str[2]-'A'+1;
    40             map[x][y]=1;
    41             indegree[y]++;
    42             int s=TopoSort(n);
    43             if(s==0) //有环
    44             {
    45                 printf("Inconsistency found after %d relations.
    ",i);
    46                 sign=1;
    47             }
    48             if(s==1) //有序
    49             {
    50                 printf("Sorted sequence determined after %d relations: ",i);
    51                 for(int j=0;j<n;j++)
    52                     printf("%c",q[j]+'A'-1);
    53                 printf(".
    ");
    54                 sign=1;
    55             }
    56         }
    57         if(!sign) //不确定
    58             printf("Sorted sequence cannot be determined.
    ");
    59     }
    60     return 0;
    61 }

     

     

  • 相关阅读:
    django ajax报错解决:You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set.
    django使用session报错:no such table: django_session
    centos7下yum安装mariadb
    pip报错解决:EnvironmentError: mysql_config not found
    django报错解决:view must be a callable or a list/tuple in the case of include().
    onpageshow和onpagehide
    深入理解Linux的CPU上下文切换
    看完就彻底懂了红黑树!红黑树的插入、删除、左旋、右旋
    shell根据csv生成sql
    shell中的EOF用法
  • 原文地址:https://www.cnblogs.com/A-FM/p/5369854.html
Copyright © 2011-2022 走看看