POJ Sorting It All Out 1094(拓扑排序)

本文介绍了一种基于拓扑排序的问题解决方法,通过处理输入的关系集合,确定是否能得出唯一的排序序列,或者是否存在矛盾的输入数据。文章详细解释了拓扑排序的概念,并提供了具体的实现代码。

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

                                                                                        Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 32801 Accepted: 11394

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.
题意:1给出m条关系,如果可以确定一个唯一的关系,则把这个关系输出来

   2如果在i条关系存在环,则输出在i条关系存在环

   3其他情况输出不确定

每给出一条关系都需要判断一下,如果有1或2的情况,则只录入,不判断

拓扑排序,用一个队列,如果度等于0,入队,然后找以这个点为前缀的点,改变度,直到队列为空

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
int v[50],tep[50],du[50],mp[50][50],k[50];
int n;
void init()
{
    for(int i=0;i<n;i++)
    {
        du[i]=0;
    }
    memset(mp,0,sizeof(mp));
}
int topsort()
{
    memset(v,0,sizeof(v));
    queue<int >q;
    while(!q.empty())q.pop();
    int i,j;
    for(i=0;i<n;i++)
    {
        if(du[i]==0)
        {
            v[i]=1;
            q.push(i);
        }
    }
    int ff=0,pos=0;
    while(!q.empty())
    {
        if(q.size()>1)ff=1;
        int t=q.front();
        q.pop();
        k[pos++]=t;
        for(i=0;i<n;i++)
        {
            if(mp[t][i])
                du[i]--;
        }
        for(i=0;i<n;i++)
        {
            if(!v[i] && du[i]==0)
            {
                v[i]=1;
                q.push(i);
            }
        }
    }
    if(pos<n) return 1;
    if(ff) return 2;
    return 3;
}
int main()
{
    int m,t,i,j;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
        {
            break;
        }
        init();
        char s[10];
        int flag=2,ff=0,stop;
        for(j=1;j<=m;j++)
        {
            scanf("%s",s);
            if(ff)continue;
            int a=s[0]-'A';
            int b=s[2]-'A';
            if(!mp[a][b])
            {
                mp[a][b]=1;
                du[b]++;
            }
            for(i=0;i<n;i++)
                tep[i]=du[i];
            flag=topsort();
            for(i=0;i<n;i++)
                du[i]=tep[i];
            if(flag!=2)
            {
                ff=1;
                stop=j;
            }
        }
        if(flag==3)
        {
            printf("Sorted sequence determined after %d relations: ",stop);
            for(i=0;i<n;i++)
            {
                printf("%c",k[i]+'A');
            }
            printf(".\n");
        }
        else if(flag==1)
        {
            printf("Inconsistency found after %d relations.\n",stop);
        }
        else
        {
            printf("Sorted sequence cannot be determined.\n");
        }
    }
}


























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值