【POJ】poj 1094(拓扑排序)

本文介绍了一个简单的图论问题,通过构建图并利用入度的概念来判断一组字母间的偏序关系是否存在矛盾,是否能确定唯一的排序顺序。对于每组输入,程序会输出相应的判断结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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<algorithm>
#include<string>
#include<stack>
using namespace std;

int ins[30],injudge[30],way[30];
int point,judge,where;
int pic[30][30];///存图
int judges()
{
    memcpy(injudge,ins,sizeof(ins));///将出入度复制过来;
    stack<int> s;
    for ( int i = 0; i < point; i++)
    {
        if ( injudge[i] == 0)
            s.push(i);///入度为0,就可以取下来为第一个;入栈;
    }
    int num = 0; int flag = 0;
    while ( !s.empty())
    {
        if ( s.size() > 1){flag = true;}///如果有两个入度0 的点那么就不能确定路线
        int temp = s.top(); s.pop();
        way[num++] = temp;///将入度为1 的值计入路线中
        for ( int i = 0; i < point;i++)
        {
            if ( pic[temp][i] && --injudge[i] == 0) s.push(i);
            ///如果有线,那么连接的点入度减去一,如果减去一之后为0 的话就入栈
        }
    }
    if ( num != point ) return 1;///有圆圈
    else if ( flag ) return 2;///不确定;
    return 3;///确定
}
int main()
{
    //freopen("in.txt","r",stdin);
    while ( scanf("%d %d",&point,&judge) != EOF )
    {
        if ( point == 0 ) break;
        memset(ins,0,sizeof(ins));
        memset(pic,0,sizeof(pic));
        string j; string ans1="";
        int flag1 = 0; int flag2 = 0;
        for ( int i = 0; i < judge; i++)
        {
            cin >> j;
            if (!flag1 && !flag2)
            {
                char a = j[0]; char b = j[1]; char c = j[2];
                if ( pic[c-'A'][a-'A'] == 1)///如果有反线就输出矛盾;
                {
                    flag2 = 1;
                    printf("Inconsistency found after %d relations.\n",i+1);
                    continue;
                }
                else
                {
                    pic[a-'A'][c-'A'] = 1;
                    ins[c-'A']++;
                }
                int flag3 = judges();///判断函数
                if ( flag3 == 3 )
                {
                    printf("Sorted sequence determined after %d relations: ",i+1);
                    for ( int k = 0; k < point; k++)
                    {
                        ans1 += way[k]+'A';
                    }
                    cout << ans1 << "." << endl;
                    flag1 = 1;
                }else if ( flag3 == 1)
                {
                    printf("Inconsistency found after %d relations.\n",i+1);
                    flag2 = 1;
                }
            }
        }
        if (!flag1&&!flag2)
            printf("Sorted sequence cannot be determined.\n");

    }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值