zoj 1060 Sorting It All Out

这题题目的意思我跟hdu的确定比赛名次的要求搞混了,其实很容易。

确定比赛名次的题意是在不确定顺序的时候,不能确定的顺序按照字母升序排列

这边给出一些大小关系,而你的任务就是理清这些关系。

然后就有一个全序,偏序的概念。

具体的理论部分可以见:http://blog.csdn.net/dm_vincent/article/details/7714519

要注意的就是题目中的要求是

环和全序得出结论后,就不再进行后面数据的处理。

在不确定顺序的情况下,环是可以存在的。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #define MAX 30
  5 //嗯 所谓拓扑排序就是
  6 //就是每次遍历所有入度为0的点,然后消除入度0点的出度边
  7 //然后一直做这个操作,直到不存在入度为0的点了,无法进行该操作就是成环了。
  8 //水题一道 ,以前都不知道原理就直接看模版都看不懂
  9 //
 10 int n,m,edge[MAX+2][MAX+2],edge1[MAX+2][MAX+2],result[MAX+1],flag;
 11 int toposort(int k)
 12 {
 13     int i,j,index,count=0;
 14     flag=0;
 15     int num=0;
 16     for(i=1; i<=n; i++)//每次都消掉一个点,所以要n层
 17     {
 18         index=num=0;
 19         for(j=1; j<=n; j++)
 20         {
 21         //找入度为0的点,这边可以完成部分拓扑排序中不确定顺序的顺序
 22         //http://acm.hdu.edu.cn/showproblem.php?pid=1285  中的提示部分
 23             if(edge1[0][j]==0)
 24             {
 25                 num++;
 26                 index=j;
 27         //break;这边是确定顺序的方法之一,每次只能按字母序排列不确定的部分
 28             }
 29         }
 30         if(num>1)  flag=3;//同一次,存在多个可以消掉的入度为0的点,不确定序列
 31         if(index>=1&&index<=n)
 32         {//消除该入度为0点
 33             edge1[0][index]--;
 34             result[count++]=index;//加入结果数组
 35             for(j=1; j<=n; j++)
 36             {
 37                 if(edge1[index][j]==1&&index!=j)
 38                 {//消除与上面入度为0点连接的点
 39                     edge1[index][j]=-1;
 40                     edge1[0][0]--;//总边数递减,下面检测环需要用
 41                     edge1[0][j]--;//和index点相连的其他点
 42                 }
 43             }
 44 
 45         }
 46         else break;//不存在入度为0的点了
 47     }
 48     if(edge1[0][0]>0)
 49     {//如果成环的话,那么在那个环的任一点里面怎么都不会出现入度为0点,边数自然会剩余
 50         return flag=2;
 51     }
 52     else if(count==n&&flag!=3)//可以定下 序列
 53     {
 54         return flag=1;
 55     }
 56     return flag=0;
 57 }
 58 int main()
 59 {
 60     int i,j,index,in,t;;
 61     char a,b;
 62     while(scanf("%d %d",&n,&m),n,m)
 63     {
 64         getchar();
 65         t=flag=0;
 66         memset(edge,-1,sizeof(edge));
 67         for(i=0; i<m; i++)
 68         {
 69             edge[0][0]=i+1;
 70             scanf("%c<%c",&a,&b);
 71             getchar();
 72             if(edge[0][a-'A'+1]==-1)
 73             {
 74                 edge[0][a-'A'+1]=0;
 75             }
 76             if(edge[0][b-'A'+1]==-1)
 77             {
 78                 edge[0][b-'A'+1]=0;
 79             }
 80             if(edge[a-'A'+1][b-'A'+1]==-1) edge[0][b-'A'+1]++;
 81             edge[a-'A'+1][b-'A'+1]=1;
 82             memcpy(edge1,edge,sizeof(edge));//数组的赋值函数
 83             if(t!=1&&t!=2)
 84             {
 85                 memcpy(edge1,edge,sizeof(edge));
 86                 t=toposort(i+1);
 87                 if(t==1||t==2) in=i;
 88             }
 89         }
 90         if(t==1)
 91         {
 92             printf("Sorted sequence determined after %d relations: ",in+1);
 93             for(i=0; i<n; i++)
 94                 printf("%c",'A'+result[i]-1);
 95             printf(".
");
 96         }
 97         else if(flag==2)
 98         {
 99             printf("Inconsistency found after %d relations.
",in+1);
100         }
101         else printf("Sorted sequence cannot be determined.
");
102     }
103     return 0;
104 }
View Code
原文地址:https://www.cnblogs.com/linxhsy/p/4291126.html