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. VmWare Workstation 10 安装 Ubuntu 14.04 问题解决

    Ubuntu安装过程很顺利,安装完成后还是有小问题存在   问题1:无法联网,PING可以通,网址无法解析 原因:默认DNS设置不正确 解决:设置DNS地址为8.8.8.8,问题解决   问题2:vm ...

  2. 一起写一个Android图片加载框架

    本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...

  3. 九幽史程博:助力国内开发者借Win10东风出海

    微软Biuld2016大会刚刚结束,会议上微软CEO纳德拉Show出的一大波黑科技,又一次让软粉们心情为之振奋,信仰充值爆棚! 尽管过去一年微软的Win10 Mobile表现不尽如人意,可是凭借PC端 ...

  4. 从走出校门到Java实习生生活

    序 男,95年,这个学期就大四了,非计算机专业(数字媒体).目前在二线城市做Java实习生,待遇一般,应该算一个正常的实习生水平吧:租的一个约10平米的小单间,实习工资-衣食住行-杂七杂八的小消费差不 ...

  5. 数据结构之链表C语言实现以及使用场景分析

    牢骚:本篇博客两个星期前已经存为草稿,鉴于发生一些糟糕的事情,今天才基本完成.本人6月份应届毕业生一枚,毕业后当天来到帝都,之后也非常顺利,面试了俩家公司都成功了.一家做C++方面电商ERP,一家做w ...

  6. 16.C#初见Lambda表达式及表达式树(九章9.1-9.3)

    在说明Lambda相关知识前,我们需要了解Lambda表达式常用于LINQ,那么我们来聊下LINQ. LINQ的基本功能就是创建操作管道,以及这些操作需要的任何状态.这些操作表示了各种关于数据的逻辑: ...

  7. JavaScript表单处理(上)

    为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面.  发文不易,转载请亲注明出处,谢谢! 一.表单介绍 在HTML中,表单是由<form& ...

  8. JavaBean ,Enterprise Bean(EJB), 三种Bean, 以及POJO

    Bean简单的理解,可以理解为组件,一组通用方法的组合: JavaBean就可以称为Java组件,是所有组件的统称,EJB称为企业级 Java组件: 三种Bean: 1). session beans ...

  9. jQuery基础之(四)jQuery创建DOM元素

    利用DOM方法创建元素节点,通常要将document.createElement().document.createTextNode().appendChild()配合使用,十分麻烦. 而jQuery ...

  10. 【poj3159】 Candies

    http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...