poj 1094 Sorting It All Out

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 32853 Accepted: 11412

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

/*转的是原创 http://blog.csdn.net/shuangde800
分析:
这题典型的拓扑排序,但是有点变化。
题目样例的三种输出分别是:
1. 在第x个关系中可以唯一的确定排序,并输出。
2. 在第x个关系中发现了有回环(Inconsisitency矛盾)
3.全部关系都没有发现上面两种情况,输出第3种.


那么对于给定的m个关系,一个个的读进去,每读进去一次就要进行一次拓扑排序,如果发现情况1和情况2,那么就不用再考虑后面的那些关系了,但是还要继续读完后面的关系(但不处理)。如果读完了所有关系,还没有出现情况1和情况2,那么就输出情况3.
拓扑排序有两种方法,一种是算法导论上的,一种是用贪心的思想,这题用贪心的思想做更好。


贪心的做法:
1. 找到所有入度为0的点, 加入队列Q
2.取出队列Q的一个点,把以这个点为起点,所有它的终点的入度都减1. 如果这个过程中发现经过减1后入度变为0的,把这个点加入队列Q。
3.重复步骤2,直到Q为空。


这个过程中,如果同时有多个点的入度为0,说明不能唯一确定关系。
如果结束之后,所得到的经过排序的点少于点的总数,那么说明有回环。




题目还需要注意的一点:如果边(u,v)之前已经输入过了,那么之后这条边都不再加入。


*/
我看了的博客,我认为它还少说了一点,就是当你已经发现序列排好了或者有环那就不必向下看了,
比如
4 7
A<B
A<C
B<C
C<D
B<D
A<B
C<B
如果单纯看前六个那就已经排好了,如果再看第七个那肯定有环了,我们发现已经排好了或已经发现有环时就不用继续向下看了,,,
所以我们就要求一步一步的看一步一步的写,不能全部储存完后再进行拓扑的,,,,





#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
vector<int> ve[102];    
queue<int> q;
int cnt;
int s[102];
int n,m;
int temp[102],in[102];
void init()
{
    int i;
    memset(in,0,sizeof(in));
    for(i = 0;i < n; i++)
    {
        ve[i].clear();
    }
}
int cha(int x,int y)    //查重
{
    int i;
    for(i = 0;i < ve[x].size();i++)
    {
        if(ve[x][i] == y)
            return 0;
    }
    return 1;
}
int topu()
{
    int i;
    while(!q.empty())    //一定要初始化
    {
        q.pop();
    }
    for(i = 0;i < n; i++)    //所有入度为0的进队列
    {
        if(in[i] == 0)
        q.push(i);
    }
    cnt = 0;
    int unsure = 0;   
    while(!q.empty())
    {
        if(q.size() != 1)   //如果入度为0多于1个那就一定不可能是确定的序列
        {
            unsure = 1;
        }
        int t = q.front();
        q.pop();               //不要忘了删除队列QQ
        s[cnt++] = t;
        for(i = 0;i < ve[t].size();i++)
        {
            in[ve[t][i]]--;  //把所有t出度的点减一
            if(in[ve[t][i]] == 0)  //找入度为0的点
            {
                q.push(ve[t][i]);
            }
        }
    }
    if(cnt < n)
        return 0;   //如果有环
    if(unsure)
        return 1;   //如果不确定
    return 2;    //确定序列
}
int main()
{
    int i;
    char X,Y,F;
    int x,y;
    int f;
    while(~scanf("%d %d",&n,&m)&&(n!=0&&m!=0))
    {
        getchar();    //记住一定要getchar()
        int flag = 1;   
        init();        //初始化
        for(i = 0;i < m; i++)
        {
            scanf("%c%c%c",&X,&F,&Y);
            getchar();
            if(flag != 1)
            {
                continue;   //出错了
            }
            x = X-'A';
            y = Y-'A';
            if(F == '<'&&cha(x,y))  //删除重复输入
            {
                ve[x].push_back(y);   //说明x可后继找到y
                in[y]++;              //y的入度加一
            }
            else if(F == '>'&&cha(y,x))
            {
                ve[y].push_back(x);
                in[x]++;
            }
            memcpy(temp,in,sizeof(in));    //储存in数组的值因为你在一步一步拓扑的时候会改变in数组的值
            flag = topu();
            if(flag != 1)
            {
                f = i;          //记录第几步出错,一出错就不用向下比较了
            }
            memcpy(in,temp,sizeof(temp));  //变回in数组原有的值
        }
        if(flag == 1)
        {
            printf("Sorted sequence cannot be determined.\n");
        }
        else if(flag == 0)
        {
            printf("Inconsistency found after %d relations.\n",f+1);
        }
        else
        {
            printf("Sorted sequence determined after %d relations: ",f+1);
            for(i = 0;i < cnt; i++)
                printf("%c",s[i]+'A');
            printf(".\n");
        }
    }
}

代码菜鸟,如有错误,请多包涵!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值