拓扑排序

基本概念

  一个有向无环图的拓扑序是将图中的顶点排成一个线性序列,使得对于图中任意一对顶点u和v,若存在边<u,v>,则线性排序列中u在v之前出现

算法实现

  1.若图中现剩余的点入度均大于0,则不存在拓扑序列,即这是一个环形的图

  2.取一个入度为0的定点u并放在序列末尾

  3.删除点u以及点u伸出的所有边,同时把与u邻接的点的入度减一

  4.如果还存在定点,再进行步骤1

  

 

例题:https://vjudge.net/contest/226513#problem/A

 


Sorting It All Out

  

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.

分析:
本题需要判断三个东西:
1.判断是否是DAG
2.判断是否能形成确定的拓扑序
3.如果能形成确定的拓扑序,则输出这个拓扑序
注意:当判断的结果是1或者3的时候,则忽略后面的输入

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,indx;
int graph[27][27];
int degree[27];
char ans[27];

int topsort()
{
    int flag=1;
    int d[27];
    for(int i=0; i<27; i++)
        d[i]=degree[i];
    queue<char> que;
    while(!que.empty())
        que.pop();
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(!d[i])
        {
            cnt++;
            que.push(i+'A');
        }
    }

    if(!cnt) return 0; //表示有环
    indx=0;
    while(!que.empty())
    {
        if(que.size()>1)
            flag=-1;
        char q=que.front();
        que.pop();
        ans[indx++]=q;
        for(int i=0; i<27; i++)
        {
            if(graph[q-'A'][i])
            {
                d[i]--;
                if(!d[i])
                {
                    que.push('A'+i);
                }
            }
        }
    }
    if(indx!=n)
        return 0;
    return flag;
}

void OutputAns()
{
    for(int i=0; i<indx; i++)
        putchar(ans[i]);
}

int main()
{
    //freopen("POJ1094.txt","w",stdout);
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        int flag=-1,loc=-1;
        memset(graph,0,sizeof(graph));
        memset(degree,0,sizeof(degree));
        char v1,v2;
        for(int i=0; i<m; i++)
        {
            getchar();
            v1=getchar();
            getchar();
            v2=getchar();
            graph[v1-'A'][v2-'A']=1;
            degree[v2-'A']+=1;
            if(flag==0||flag==1)
                continue;
            flag=topsort();
            if(flag==0)
            {
                loc=i+1;
            }
            if(flag==1)
            {
                loc=i+1;
            }
        }


        if(flag==0)
        {
            printf("Inconsistency found after %d relations.\n",loc);
        }
        else if(flag==-1)
        {
            printf("Sorted sequence cannot be determined.\n");
        }
        else
        {
            printf("Sorted sequence determined after %d relations: ",loc);
            OutputAns();
            putchar('.');
            putchar('\n');
        }
    }

}
View Code

 

posted @ 2018-05-02 17:34  Lewin的成长之路  阅读(215)  评论(0编辑  收藏  举报