Sorting It All Out(简单的拓扑排序)

题目来源:[NWPU][2014][TRN][16]图论拓扑排序  A 题

http://vjudge.net/contest/view.action?cid=51448#problem/A



作者:npufz

题目:

A - Sorting It All Out
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int map1[30][30];
int rudu[30];
int chudu[30];
int visit[30];
char  okstr[30];

bool  floyd (int n)
{
    int i,j,k;
   for( i=0;i<n;i++)
    for( j=0;j<n;j++)
    for(k=0;k<n;k++)
    if(map1[j][i]&&map1[i][k])
    map1[j][k]=1;
   for(i=0;i<n;i++)
   if(map1[i][i]==1)
   return true;
return false;
}
bool  oksort(int n)
{
   int i,j;
   memset(rudu,0,sizeof(rudu));
   memset(chudu,0,sizeof(chudu));
   for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    if(map1[i][j])
   {
       rudu[j]++;
       chudu[i]++;
   }
   for(i=0;i<n;i++)
    if(rudu[i]+chudu[i]!=(n-1))
    return false;
   return  true;
}
int  tuopusort(int n)
{
    int i,dijige=0,jiaobiao,jb;
    bool flag;
    memset(visit,0,sizeof(visit));
    for(i=0;i<n;i++)
    {
        if(rudu[i]==0)
        {
            jiaobiao=i;
            flag=true;
        }
    }
    while(flag)
    {
        okstr[dijige]=jiaobiao+'A';
        dijige++;
        visit[jiaobiao]=-1;
        jb=jiaobiao;
        flag=false;
        for(i=0;i<n;i++)
        {
            if(map1[jb][i]&&visit[i]==0) rudu[i]--;
            if(visit[i]==0&&rudu[i]==0)
            {
                jiaobiao=i;
                flag=true;
            }
        }
    }
    okstr[dijige]='\0';
    return 0;
}
int main()
{
    int n,m,flag1,flag2;
    char  s1[10];
    while(scanf("%d%d",&n,&m),n!=0&&m!=0)
    {
      for(int i=0;i<30;i++)   for(int j=0;j<30;j++)
        map1[i][j]=0;
        flag1=0;
        flag2=0;
       for(int i=1;i<=m;i++)
      {
       scanf("%s",s1);
       map1[s1[0]-'A'][s1[2]-'A']=1;
       if(flag1||flag2)  continue;
       else if(floyd(n))
       {
           flag1=i;
           continue;
       }
       else  if(oksort(n))
       {
           flag2=i;
           tuopusort(n);
       }
      }
    if(flag1)
         printf("Inconsistency found after %d relations.\n",flag1);
    else if(flag2)
         printf("Sorted sequence determined after %d relations: %s.\n",flag2,okstr);
    else
        printf("Sorted sequence cannot be determined.\n");
    }
 return 0;
}

总结:

一开始对拓扑排序基本没有思路,就找了一份大神的代码读了一遍,顿时觉得明白了不少,首先确定到底有没有封闭的回路,这个用FLOYD判断很巧妙,也使得我对邻接矩阵有了进一步的认识,它通过一个三重循环把一个点能够到达另一个的条件充分利用,把所有的从一个点出发能够到达另一个点的关系全部刻画在邻接矩阵中,这样操作完后,再对每一个点判断它是否能够走回它自己,进而来确定它到底有没有回路,如果没有回路就对它进一步的判断它的拓扑序是否唯一,如果没有回路又拓扑序唯一,那么可以证明每个点的入度和出度的和恰恰为也只能为总点数减去一,如果每个点都满足上述条件,就可以对它进行拓扑排序,得到唯一的拓扑序列,在数据的维护中,每个点的入度和出度以及邻接矩阵的维护很重要,因为整个图的信息都含在邻接矩阵中,而每个点的入度和出度单独进行维护而不是每次用到再从矩阵中求算是很节省时间也很方便的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值