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. Jenkins进阶系列之——16一个完整的JENKINS下的ANT BUILD.XML文件

    网上看见的,确实很全,该有的基本都覆盖到了.自己拿来稍微改改就可以用了. 注:property中的value是你自己的一些本地变量.需要改成自己的 <?xml version="1.0 ...

  2. WINDOWS下用脚本运行redis和mongodb

    开发环境每次开麻烦,又不想建service,用bat最简单 @echo off echo 打开NOSLQ服务 start E:\nosql\mongodb\mongod.exe -dbpath e:\ ...

  3. fis-receiver:一行命令将项目部署到远程服务器

    前言 本项目基于FIS2,没了.其实fis项目本身就提供了php版本的范例,这里翻译成node版本. 项目地址:https://github.com/chyingp/fis-receiver 服务端接 ...

  4. android之文件权限问题

    当我们在手机上安装一个应用的时候,linux会为每个APP创建一个用户名和用户组 xidian.dy.com.chujia是系统为每个应用创建的一个独立的文件夹,我们可以看到这个文件的所有者为app_ ...

  5. xml基本操作

    在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...

  6. [转载]NSString中判断中文,英文,数字

    曾有需求做个用户名中非法字符的判断,要求是只能输入中英文和数字,其他字符一律非法,故写了下边一个程序mark一下吧~~ NSString *testString = @"春1mianBU觉晓 ...

  7. 每天一个linux命令(28):diff 命令

    diff 命 令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版 本的diff还支持二进制文件.diff程 ...

  8. 0505--鲜花售卖网之“NABCD模型”

    一.NABCD 1) N (Need 需求)--(分析人:梁植淋) --简介 我们的鲜花售卖系统主要是给需要买花或者是养花的顾客提供一个购花平台,买花时通过位置识别给用户提供附近的花店购花,花店接到订 ...

  9. iOS边练边学--Http网络再学习,简单介绍

    一.URL 什么是URL URL中常见的协议 二.Http Http的基本通信过程 发送Http请求的方法 GET 和 POST 对比 GET 和 POST 的选择 三.iOS中的Http学习 iOS ...

  10. 关于obj和基本类通过函数参数传进去执行是否改变原来的值

    var obj = { p1 : 1, p2 : 2 }; (function(_/* 这个东东是地址的应用哦 */){ _.p1 = 3, _.p2 = 4 })(obj) var i = 2; ( ...