Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28762   Accepted: 9964

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

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n , m ;
bool map[][] ;
int in[] ;
char st[][] ;
char a[] ; int topo ()
{
queue <int> q ;
int indegree[] ;
int cnt = ;
int k = ;
int ans = - ;//record the condition that "cnt > 1"
while (!q.empty ())
q.pop () ;
for (int i = ; i <= n ; i++)
indegree[i] = in[i] ;
for (int i = ; i <= n ; i++) {
if (in[i] == ) {
q.push (i) ;
cnt++ ;
}
// printf ("%d " , in[i]) ;
}
if (cnt > )
ans = ;//conditons are not satisfied
while (!q.empty ()) {
int temp = q.front () ;
a[k++] = 'A' + temp - ;
q.pop () ;
cnt = ;
for (int i = ; i <= n ; i++) {
if (map[temp][i]) {
indegree[i]-- ;
if (indegree[i] == ) {
cnt++ ;
q.push (i);
}
}
}
if (cnt > )
ans = ;//conditons are not satisfied
}
if (k != n )
return ;//there is a circle
if (k == n && ans != )
return ;//success if (ans == )
return ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
int link ;
int flag ;
int success ;
while (~ scanf ("%d%d" , &n , &m)) {
if (n == && m ==)
break ;
getchar () ;
memset (in , , sizeof(in)) ;
memset (map , , sizeof(map)) ;
link = - ;
flag = - ;
success = - ;
for (int i = ; i < m ; i++)
gets (st[i]) ; for (int i = ; i < m ; i++) {
int u = st[i][] - 'A' + ;
int v = st[i][] - 'A' + ;
// printf ("u = %d , v = %d\n" , u , v) ;
if (!map[u][v])
in[v]++ ;
map[u][v] = ; flag = topo () ;
if (flag == ) {
link = i + ;
break ;
}
else if(flag == ) {
success = i + ;
break ;
}
// puts ("") ;
}
if (flag == ) {
printf ("Inconsistency found after %d relations.\n" , link) ;
}
else if (flag == ) {
puts ("Sorted sequence cannot be determined.") ;
}
else {
printf ("Sorted sequence determined after %d relations: " , success) ;
for (int i = ; i < n ; i++) {
printf ("%c" , a[i]) ;
}
printf (".\n") ;
}
}
return ;
}

要一条条边测下来,not determined 肯定是最后一条边出来才能 判断 ,但在这之前若 先判断出了 success 或 inconsistency 就能结束了(一开始要把所有边先记录下来)

success只有 无环 & 同一时刻加入队列的点 == 1 时才能 成立

poj.1094.Sorting It All Out(topo)的更多相关文章

  1. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

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

  2. poj 1094 Sorting It All Out(nyoj 349)

    点击打开链接 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24544   Accep ...

  3. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

  4. POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)

    ( ̄▽ ̄)" //判环:当入度为0的顶点==0时,则有环(inconsistency) //判序:当入度为0的顶点仅为1时,则能得到有序的拓扑排序,否则无序 //边输入边判断,用contin ...

  5. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

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

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

    https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...

  7. 题解报告:poj 1094 Sorting It All Out(拓扑排序)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  8. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  9. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

随机推荐

  1. Stream 流操作

     Stream 类 先看下面的图 Stream 是所有流的抽象基类(不能被实例化,需要使用他的派生类FileStream/MemoryStream/BufferedStream).流是字节序列的抽象概 ...

  2. 由外边距合并到BFC

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<JavaScript实现Ajax小结> 作者主页:myvin 博主QQ:851399101(点击QQ和 ...

  3. Orchard 刨析:前奏曲

    Orchard中大量使用了依赖注入,而实现依赖注入的组件就是Autofac,它在Orchard中扮演者非常重要的角色,多租户如是,模块如是,工作区也如是.今天就来讲讲Autofac在Orchard中的 ...

  4. Unity Networking API文档翻译(二):The High Level API

    高级API (HLAPI) 是用来提供给Unity 创建多人在线游戏的组件.它是在底层传输层的基础上构建的, 对多人在线游戏提供了很多通用的功能.当传输层支持各种网络拓扑结构的时候,HLAPI是一个功 ...

  5. 游戏服务器端引擎--DogSE的设计

    就DogSE的设计目标来说,它定位为千人左右的页游服务器,在不修改任何底层模块的情况下可以快速的写各种游戏业务.就算是新人在熟悉2~3天后也可以开始写一个游戏. 项目可以从github获得,访问地址: ...

  6. 使用TinkPHP实现品字形布局

    一.后台管理模版 后台管理模版通常使用frameset/iframe来布局.例如: <!DOCTYPE html> <html> <head> <title& ...

  7. 工作的思考十五:升职前需要做的准备(TeamLeader)

    当一个人在公司的工作年限以及经验的积累到达一个程度的时候,升职其实是件高兴的事,但面临角色的转变需要提前做些准备的. 其实如果你对你的职业规划很清楚的话,那么你就应该在升职之前就会开始进行角色的转换. ...

  8. C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)

    前言 这一节主要来了解一下类和结构体之间的异同点.以及针对String和StringBuilder的用法.equals和==,其实可以看出很多地方都用到了上一节的值类型和引用类型.堆栈和装箱拆箱操作吧 ...

  9. 第二章:javascript: 数组

    数组是编程世界里最常见的数据结构.任何一种编程语言都包含数组,只是形式稍微有差异.数组是编程语言中的内建类型,通常效率都很高.可以满足不同需求的数据存储,本章将探索javascript中的数组工作原理 ...

  10. u11-nav02

    header:before, header:after ,.navigation:before, .navigation:after,.nav-row:before, .nav-row:after,. ...