Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29744   Accepted: 10293

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<iostream>
#include<algorithm>
using namespace std;
int map[100][100];
int m,n;
int tindegree[100],indegree[100];
char str[5];
char s[39];
int toposort(){
bool flag=true;
memset(tindegree,0,sizeof(tindegree));
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++){
tindegree[i]=indegree[i];
}
for(int i=1;i<=n;i++){
int sum=0,k;
for(int j=1;j<=n;j++){
if(!tindegree[j]){
k=j;
sum++;
}
}
if(sum==0){///入度为0,则所剩图为一个环,无法判断了。直接可以返回,
return -1;
}
if(sum>1){///当出现入度为0的点有多个时候,可能判断不了,但是也不能直接返回0,因为带环循环会优先于
///无法判断这种情况,所以需要继续执行该函数,看还有没有死循环为环的情况
flag=false;
}
s[i-1]=k+'A'-1;
tindegree[k]--;
for(int z=1;z<=n;z++){
if(map[k][z]){
tindegree[z]--;
}
} }
s[n]='\0';///s字符串结束标志
if(flag==false)
return 0;
return 1;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)
break;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
memset(str,0,sizeof(str));
memset(s,0,sizeof(s));
int ans=2;
int temp;
bool flag=true;
for(int i=1;i<=m;i++){
scanf("%s",str);
if(flag==false)
continue;
int u=str[0]-'A'+1;
int v=str[2]-'A'+1;
if(!map[u][v]){
map[u][v]=1;
indegree[v]++;
}
ans=toposort();
if(ans==-1||ans==1){///此判断语句特别注意,只能记录状态,不能退出,需要继续读边
temp=i;
flag=false;
}
}
if(ans==1)
printf("Sorted sequence determined after %d relations: %s.\n",temp,s);
else if(ans==-1)
printf("Inconsistency found after %d relations.\n",temp);
else if(ans==2)
printf("Sorted sequence cannot be determined.\n"); }
return 0;
}

poj1094 拓扑 Sorting It All Out的更多相关文章

  1. POJ1094 拓扑排序

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

  2. POJ1094——拓扑排序和它的唯一性

    比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...

  3. poj1094拓扑排序

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

  4. poj1094 拓扑排序(出度入度简单使用)

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

  5. poj1094 拓扑序

    题意:现在有多个大写字母(不一定连续),给出字母之间的大小关系,问到第几个关系时就能判断有唯一大小排序或出现矛盾,或是有多个合理排序,若有唯一排序,则输出它. 拓扑序,只不过坑爹的是如果关系处理到一半 ...

  6. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

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

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

  8. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

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

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

随机推荐

  1. Linq之Expression高级篇(常用表达式类型)

    目录 写在前面 系列文章 变量表达式 常量表达式 条件表达式 赋值表达式 二元运算符表达式 一元运算符表达式 循环表达式 块表达式 总结 写在前面 首先回顾一下上篇文章的内容,上篇文章介绍了表达式树的 ...

  2. 每天一个linux命令(52):scp命令

    scp 是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且 scp传输是加密的.可能会稍微影响一下速度.当你服 ...

  3. Android Fresco (Facebook开源的图片加载管理库)

    Fresco是Facebook开源的一个图片加载和管理库. 这里是Fresco的GitHub网址. 同类型的开源库市面有非常多,比如Picasso, Universal Image Loader, G ...

  4. Log4Net使用详解

    1.Log4Net环境的搭建与基本配置 (1)Log4Net框架介绍     Log4net 是 Apache 下一个开放源码的项目,它是Log4j 的一个克隆版.我们可以控制日志信息的输出目的地.L ...

  5. 用php生成数据字典

    <?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...

  6. 001-编译hadoop-2.5.2总结

    前两天废了很大的劲来对hadoop-2.5.2进行64位系统的手动编译,由于对linux系统环境以及hadoop本身的不熟悉,编译过程中也出现了很多的问题,在此记录一下,对自己以后再次编译和看到此文章 ...

  7. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  8. POJ3579 Median

    Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...

  9. java中的不为空判断

    String不为空判断 if(null != str && !"".equals(str)) List不为空判断 if(list!=null && ...

  10. IOS基础之 (十五)知识点

    一 SEL 1. 方法的存储位置 每个类的方法地址列表都存储在类对象中. 每个方法都有一个与之对应的SEL类型的对象. 根据一个SEL对象就可以找到方法的地址,进而调用方法. Person.h #im ...