POJ 1094 Sorting It All Out【拓扑排序】

Sorting It All Out

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 31994

 

Accepted: 11117

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.

Source

East Central North America 2001

 

题目大意:给你n个字母和这么些个关系,让你确定这些字母之间的关系是否会出现环,或者是找不到明确的排名关系,也或者是让你输出排名顺序。


具体分析:题干要求的是每知道一个关系,就进行一次判断,如果当前关系加入之后能够找到所有字母的排名关系,那么就输出排名关系。如果找到了矛盾的环,比如样例2一样,就输出会出现矛盾,如果整个过程没有出现矛盾或者是能够找到排名顺序,输出Sorted sequence cannot be determined.。


这个题也可以用传递闭包(floyd实现)+拓扑排序来解决。这里提供思路和AC代码:http://blog.csdn.net/mengxiang000000/article/details/51345744


这一次使用拓扑排序的整体思路来解决、

思路:

每一次入边都进行一次拓扑排序,想想拓扑排序完成的条件:没有有向环。辣么很容易就能想到如何处理有环的情况,如果拓扑排序之后拆出的点不足n个,那么就说明整个有向图里边会有环。


那么如何判断排名关系不唯一的情况呢?其实排名关系不唯一的情况也就是有并列关系的情况,也就是在说在拓扑过程中,如果某一次拆一个点的关联边的时候一下子拆出两个或者两个以上的度为0的点,其实也就是说排名不唯一的情况。


处理完这两个点,题也就能够简单解出来了。

注意两个点:

1、不要少输出句号

2、要先判断是否成环,再判断排序关系是否唯一。


AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zuobiao
{
    int x,y;
}a[10000];
int degree[50];
int map[50][50];
int vis[50];
int ans[50];
int n,m;
int  solve(int num)
{
    memset(ans,0,sizeof(ans));
    memset(vis,0,sizeof(vis));
    queue<int >s;
    int degree2[50];
    for(int i=0;i<n;i++)
    {
        degree2[i]=degree[i];
    }
    int cont=0;
    for(int i=0;i<n;i++)
    {
        if(degree2[i]==0)
        {
            s.push(i);
            vis[i]=1;
            ans[cont++]=i;
        }
    }
    int flag=0;
    if(s.size()==0)return -1;
    if(s.size()>1)flag=1;
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        int tmp=cont;
        for(int j=0;j<n;j++)
        {
            if(map[u][j]==1&&vis[j]==0)
            {
                degree2[j]--;
                if(degree2[j]==0)
                {
                    s.push(j);
                    vis[j]=1;
                    ans[tmp++]=j;
                }
            }
        }
        if(tmp>cont+1)flag=1;
        cont=tmp;
    }
    if(cont<n)return -1;
    else if(flag==1)return 0;
    else
    {
        printf("Sorted sequence determined after %d relations: ",num+1);
        for(int i=0;i<cont;i++)
        {
            printf("%c",ans[i]+'A');
        }
        printf(".\n");
        return 1;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n+m==0)break;
        memset(map,0,sizeof(map));
        memset(degree,0,sizeof(degree));
        for(int i=0;i<m;i++)
        {
            char tmp[10];
            scanf("%s",tmp);
            a[i].x=tmp[0]-'A';
            a[i].y=tmp[2]-'A';
        }
        int i;
        for(i=0;i<m;i++)
        {
            int u=a[i].x;
            int v=a[i].y;
            map[u][v]=1;
            degree[v]++;
            int d=solve(i);
            if(d==-1)
            {
                printf("Inconsistency found after %d relations.\n",i+1);
                break;
            }
            if(d==1)break;
        }
        if(i==m)
        {
            printf("Sorted sequence cannot be determined.\n");
        }
    }
}







你可能感兴趣的:(poj,pku,1094,1094)