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

  1. 4 6
  2. A<B
  3. A<C
  4. B<C
  5. C<D
  6. B<D
  7. A<B
  8. 3 2
  9. A<B
  10. B<A
  11. 26 1
  12. A<Z
  13. 0 0

Sample Output

  1. Sorted sequence determined after 4 relations: ABCD.
  2. Inconsistency found after 2 relations.
  3. Sorted sequence cannot be determined.

Source

 
 
代码
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int map[100][100];
  7. int m,n;
  8. int tindegree[100],indegree[100];
  9. char str[5];
  10. char s[39];
  11. int toposort(){
  12. bool flag=true;
  13. memset(tindegree,0,sizeof(tindegree));
  14. memset(s,0,sizeof(s));
  15. for(int i=1;i<=n;i++){
  16. tindegree[i]=indegree[i];
  17. }
  18. for(int i=1;i<=n;i++){
  19. int sum=0,k;
  20. for(int j=1;j<=n;j++){
  21. if(!tindegree[j]){
  22. k=j;
  23. sum++;
  24. }
  25. }
  26. if(sum==0){///入度为0,则所剩图为一个环,无法判断了。直接可以返回,
  27. return -1;
  28. }
  29. if(sum>1){///当出现入度为0的点有多个时候,可能判断不了,但是也不能直接返回0,因为带环循环会优先于
  30. ///无法判断这种情况,所以需要继续执行该函数,看还有没有死循环为环的情况
  31. flag=false;
  32. }
  33. s[i-1]=k+'A'-1;
  34. tindegree[k]--;
  35. for(int z=1;z<=n;z++){
  36. if(map[k][z]){
  37. tindegree[z]--;
  38. }
  39. }
  40.  
  41. }
  42. s[n]='\0';///s字符串结束标志
  43. if(flag==false)
  44. return 0;
  45. return 1;
  46. }
  47. int main(){
  48. while(scanf("%d%d",&n,&m)!=EOF){
  49. if(n==0&&m==0)
  50. break;
  51. memset(map,0,sizeof(map));
  52. memset(indegree,0,sizeof(indegree));
  53. memset(str,0,sizeof(str));
  54. memset(s,0,sizeof(s));
  55. int ans=2;
  56. int temp;
  57. bool flag=true;
  58. for(int i=1;i<=m;i++){
  59. scanf("%s",str);
  60. if(flag==false)
  61. continue;
  62. int u=str[0]-'A'+1;
  63. int v=str[2]-'A'+1;
  64. if(!map[u][v]){
  65. map[u][v]=1;
  66. indegree[v]++;
  67. }
  68. ans=toposort();
  69. if(ans==-1||ans==1){///此判断语句特别注意,只能记录状态,不能退出,需要继续读边
  70. temp=i;
  71. flag=false;
  72. }
  73. }
  74. if(ans==1)
  75. printf("Sorted sequence determined after %d relations: %s.\n",temp,s);
  76. else if(ans==-1)
  77. printf("Inconsistency found after %d relations.\n",temp);
  78. else if(ans==2)
  79. printf("Sorted sequence cannot be determined.\n");
  80.  
  81. }
  82. return 0;
  83. }

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. 标准I/O

    在程序运行时,会默认为我们打开三个流:标准输入流.标准输出流.标准出错流. 标准输入流一般对应我们的键盘 标准输出流一般对应显示器 标准出错流一般也对应显示器 1.标准输入流 在标准I/O中,java ...

  2. [设计模式] Javascript 之 外观模式

    外观模式说明 说明:外观模式是用于由于子系统或程序组成较复杂而提供的一个高层界面接口,使用客户端更容易访问底层的程序或系统接口; 外观模式是我们经常使用遇到的模式,我们经常涉及到的功能,可能需要涉及到 ...

  3. Ibatis学习总结3--SQL Map XML 映射文件

    在前面的例子中,只使用了 SQL Map 最简单的形式.SQL Map 的结构中还有其他更多 的选项.这里是一个 mapped statement 较复杂的例子,使用了更多的特性. <sqlMa ...

  4. JQuery学习(1)

    JQuery学前准备 JQuery的各种包: 1.jquery-ui(包含小工具及组件) 2.jquery-1.7.1.intellisense.js(智能提示包) 3.jquery-1.7.1.js ...

  5. LightOJ1348 树链剖分

    简单题,看题目就懂. #include<queue> #include<stack> #include<cmath> #include<cstdio> ...

  6. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

  7. sort关于去除重复/查找非重复/查找重复/统计

    去除重复sort file |uniq   查找非重复 sort file | uniq -u   查找重复 sort file | uniq -d   统计 sort file | uniq -c

  8. Java编程思想学习(五) 复用类

    1.继承与组合 复用类的方法有两种:继承与组合.继承就不多说了,组合就是直接在类中new一个对象. 数组也是对象,使用数组也是组合的一种. 2.初始化基类 当创建一个导出类的对象时,该对象包含一个基类 ...

  9. SQL Server数据库还原:"因为数据库正在使用,所以无法获得对数据库的独占访问权"

    如题,网上找了一些客套的方法,如果不想去折腾,请看我的方法: 1.先脱机数据库,这个目的就是为了停掉所有链接 2.选择还原数据库,如果提示日志尾部不完整,请选择数据库属性的选项,覆盖现有数据. 还可以 ...

  10. 走进科学 WAF(Web Appllication Firewall)

    1. 前言 当WEB应用越来越为丰富的同时,WEB 服务器以其强大的计算能力.处理性能及蕴含的较高价值逐渐成为主要攻击目标.SQL注入.网页篡改.网页挂马等安全事件,频繁发生. 企业等用户一般采用防火 ...