1. 程式人生 > >Sorting It All Out(簡單的拓撲排序)

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判斷很巧妙,也使得我對鄰接矩陣有了進一步的認識,它通過一個三重迴圈把一個點能夠到達另一個的條件充分利用,把所有的從一個點出發能夠到達另一個點的關係全部刻畫在鄰接矩陣中,這樣操作完後,再對每一個點判斷它是否能夠走回它自己,進而來確定它到底有沒有迴路,如果沒有迴路就對它進一步的判斷它的拓撲序是否唯一,如果沒有迴路又拓撲序唯一,那麼可以證明每個點的入度和出度的和恰恰為也只能為總點數減去一,如果每個點都滿足上述條件,就可以對它進行拓撲排序,得到唯一的拓撲序列,在資料的維護中,每個點的入度和出度以及鄰接矩陣的維護很重要,因為整個圖的資訊都含在鄰接矩陣中,而每個點的入度和出度單獨進行維護而不是每次用到再從矩陣中求算是很節省時間也很方便的。