Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32275   Accepted: 11210

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.

题目大意:给你字母表的前N个字母以及M个他们之间的关系,问最终是全序,偏序,还是出现矛盾。

思路分析:有向图,很明显拓扑排序,这是我做的第二道拓扑排序的题目,在学习图论之前一直以为拓扑

排序就是单纯的一种排序方式,现在才知道它是图论的一部分,有向图,因为本题不仅要求输出结果,还要判断

在输入第几个顺序时得出的结果,因此每输入一组数据就要进行一拓扑排序,拓扑排序本质上一种偏序排序,

当然在一定情况下会出现全序的特殊情况,j!=n,就说明有向图的构建过程中出现了环,有可能直接是反向环,

也有可能是绕一圈后回来,如果队列中元素>1,说明队列中存在两个以上优先级相同的元素,即有向图是偏序的。

不说了,上代码,本题对于拓扑排序的理解还是十分有用的。

代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=27;
int indegree[maxn],ans[maxn],graph[maxn][maxn];
int n,m;
int topsort()
{
    int flag=0;
    int in[maxn];
    int j=0;
    memcpy(in,indegree,sizeof(indegree));//一定要重新定义一个数组,防止对主函数中indegree数组产生影响
    queue<int> q;
    for(int i=0;i<n;i++)
    {
        if(in[i]==0) q.push(i);//将入度为0的点放入队列
    }
    while(!q.empty())
    {
        if(q.size()>1)  flag=1;
        int t=q.front();
        q.pop();//记得弹出
        ans[j++]=t;
        for(int i=0;i<n;i++)
        {
            if(graph[t][i])
            {
                in[i]--;
                if(in[i]==0) q.push(i);
            }
        }
    }
    if(j!=n)//不能拓扑排序 ,存在环
        return 1;
    else if(flag==1) return 2;
    else return 0;
}
int main()
{
    char s[5];
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        int incon=0,detmin=0;
        memset(graph,0,sizeof(graph));
        memset(indegree,0,sizeof(indegree));
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s);
            if(detmin||incon) continue;
            int a=s[0]-'A';
            int b=s[2]-'A';
            if(graph[a][b]==0)
            {
                graph[a][b]=1;
                indegree[b]++;
            }
            int res=topsort();
            if(res==0)
            {
                detmin=1;
                printf("Sorted sequence determined after %d relations: ",i);
                for(int i=0;i<n;i++)
                printf("%c",ans[i]+'A');
                printf(".\n");
            }
            if(res==1)
            {
                incon=1;
                printf("Inconsistency found after %d relations.\n",i);
            }
        }
        if(!incon&&!detmin) printf("Sorted sequence cannot be determined.\n");
    }

}

poj1094 topsort的更多相关文章

  1. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

  2. POJ1094[有向环 拓扑排序]

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33184   Accepted: 11 ...

  3. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  4. POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)

    题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到 ...

  5. POJ1094 拓扑排序

    问题:POJ1094   本题考查拓扑排序算法   拓扑排序:   1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组.   循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...

  6. POJ1094 Sorting It All Out(拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30110   Accepted: 10 ...

  7. POJ - 3249 Test for Job (DAG+topsort)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  8. 拓扑排序 topsort详解

    1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { marg ...

  9. poj 3648 2-SAT建图+topsort输出结果

    其实2-SAT类型题目的类型比较明确,基本模型差不多是对于n组对称的点,通过给出的限制条件建图连边,然后通过缩点和判断冲突来解决问题.要注意的是在topsort输出结果的时候,缩点后建图需要反向连边, ...

随机推荐

  1. VC++深入详解读书笔记-第六章菜单

    1.MFC都是采用大写字母来标识资源ID号. 2.程序中会用到多种资源,在为资源确定其ID号时,为了明确区分资源类型,一般都遵循这样一个原则:在“ID”字符串后加上一个标识资源类型的字母,例如,我们给 ...

  2. [Mugeda HTML5技术教程之2] Mugeda HTML5富媒体平台简介

    [Mugeda HTML5技术教程之2] Mugeda HTML5动画平台简介 摘要:Mugeda提供基于云的平台,供开发人员和设计人员快速的开发.发布和统计基于HTML5的,包含丰富动画和交互的移动 ...

  3. Python新手学习基础之函数-关键字参数

    关键字参数 函数也可以使用 键 =值 的关键字参数形式被调用,这种调用形式的参数被称为关键字参数. 特别是在一个有许多参数的函数下,你想要指定参数中的部分参数,你可以使用关键字参数的方式来调用函数. ...

  4. HBase笔记--安装及启动过程中的问题

    1.使用hbase shell的时候运行命令执行失败 例如:在shell下执行 status,失败. 可能的原因:节点之间的时间差距过大 解决方法调整两个节点的时间,使二者一致,这里用了个比较笨的方法 ...

  5. [工具] 解决sublime text运行javascript console无输出问题

    1.使用nodeJS在sublime text 运行javascript 下载安装nodeJS 在sublime text新建build system:tools->build system-& ...

  6. github三大步骤

    1)git init : 初始化当前目录,把这个目录变成Git可以管理的目录 2)git add [文件名称]:  把文件添加到仓库 3)git commit -m "对当前提交文件的描述& ...

  7. Android XML文档解析(一)——SAX解析

    ---------------------------------------------------------------------------------------------------- ...

  8. OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程

    OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程 OpenJTAG与JLink的区别比较: 相同点:都同时具备USB转JTAG.USB转串口功能 差别: 1. 操作系统: O ...

  9. 【转】 ubuntu12.04更新源

    原文网址:http://blog.chinaunix.net/uid-26404477-id-3382633.html 摘 要:本文列出ubuntu 12.04 LTS更新源列表,内容为网友整理,此处 ...

  10. bzoj4003

    http://www.lydsy.com/JudgeOnline/problem.php?id=4003 可合并堆. 每个点都有一个小根堆,记住可以到这个点的骑士有哪些,以战斗力为关键字. 从底层到顶 ...