poj 1094

大二开学太忙了,强忍住学习web开发做黄色网站的冲动来看个程序题...

这个题算是我看过的第一个拓扑排序题吧,拓扑排序就是通过不断完善子集的关系来寻找全部元素的互相之间的对应关系,

以下内容出自  百度百科

由AOV网构造拓扑序列的拓扑排序算法主要是循环执行以下两步,直到不存在入度为0的顶点为止。

(1) 选择一个入度为0的顶点并输出之;

(2) 从网中删除此顶点及所有出边。

循环结束后,若输出的顶点数小于网中的顶点数,则输出“有回路”信息,否则输出的顶点序列就是一种拓扑序列。(因为一个回路上所有的点的入度都为1)

题目如下

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<vector>
using namespace std;
int b[27],q[27];
int n,m,i,x,y;
vector<int>tu[27];
char ans[27];
int tuopu()
{
    int in[27],ret=1;
    memcpy(in,b,sizeof(in));//由于需要对入度数组做更改所以先把数据存到另外一个数组中
    int front=0,rear=0;
    for(int i=0;i<n;++i)
    if(in[i]==0)q[rear++]=i;
    while(front<rear)
    {
        if(rear-front>1)ret=0;//还有不能确定排列顺序的数,因为目前有大于等于两个优先级最高的数
        int cur=q[front];
        ans[front++]='A'+cur;
        for(int i=0;i<tu[cur].size();++i)
        {
            int j=tu[cur][i];
            if(--in[j]==0)q[rear++]=j;
        }
    }
    if(front<n)return -1;//有回路,因为没有全部输出
    ans[front]=0;
    return ret;
}
int main()
{
    int flag;//判断是否能判断关系或者已经出现了回路
    char x1,x2;
    while(scanf("%d%d",&n,&m),m||n)
    {
        for(i=0;i<n;++i)tu[i].clear();
        memset(b,0,sizeof(b));
        flag=0;
        for(i=0;i<m;++i)
            {   getchar();
                scanf("%c%*c%c",&x1,&x2);
                if(flag)continue;
                x=x1-'A';y=x2-'A';
                ++b[y];//入度加1
                tu[x].push_back(y);
                flag=tuopu();
                if(flag==1)printf("Sorted sequence determined after %d relations: %s.\n",i+1,ans);
                if(flag==-1)printf("Inconsistency found after %d relations.\n",i+1);
            }
        if(!flag)puts("Sorted sequence cannot be determined.");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值