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

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

解题思路:

拓扑排序的应用。參考http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html做的。

本题须要注意的问题非常多,有点“坑”。以下是从上面博文中转的,()里面的内容是我自己加的。

题意:给你一些大写字母间的偏序关系,然后让你推断是否能唯一确定它们之间的关系,或者所给关系是矛盾的,或者到最后也不能确定它们之间的关系。

分析:

用拓扑排序:

1.拓扑排序能够用栈来实现,每次入栈的是入度为0的节点(也能够用队列,或者不使用队列和栈,循环n次,找入度为0的点)。

1.拓扑排序的结果一般分为三种情况:1、能够推断(拓扑排序有唯一的结果) 2、有环出现了矛盾(出现了没有入度为0的节点) 3、条件不足,不能推断.

2.这道题不仅须要推断这三种情况,并且还要推断在处理第几个关系时出现前两种情况,对于本道题来说三种情况是有优先级的。前两种情况是平等的谁先出现先输出谁的对应结果,对于第三种情况是在前两种情况下都没有的前提下输出对应结果的.

网上对于这道题的错误提示(须要注意):

1.本题顺序:

a.先判有没有环,有环就直接输出不能确定;

b.假设没有环,那么就看会不会有多种情况,假设有多种情况就再读下一行;假设所有行读完还是有多种情况,就是确定不了;

c.假设最后没有环,也不存在多种情况(即每次取出来入度为零的点仅仅有一个),那么才是答案;

2.有答案就先出答案,无论后面的会不会矛盾什么的;

3.假设在没有读全然部输入就能出答案,一定要把剩下的行都读完。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <stack>
  5. #include <queue>
  6. using namespace std;
  7. int indegree[30];//保存入度
  8. int graph[30][30];//是否有边
  9. char output[30];//输出可确定序列
  10. bool ok;//能够被确定
  11. bool dilemma;//有环,矛盾
  12. bool no;//不能被确定
  13. char c1,c,c2;//输入
  14.  
  15. int topo(int n)
  16. {
  17. int in[30];
  18. for(int i=0;i<n;i++)
  19. in[i]=indegree[i];//使用备用数组进行拓扑排序
  20.  
  21. stack<int>s;//入度为0的点进栈
  22. for(int i=0;i<n;i++)
  23. if(!in[i])
  24. s.push(i);
  25.  
  26. bool flag=0;//栈里面入度为0的元素大于一个时,不确定的拓扑排序
  27. int cnt=0;//入度为0的元素个数,也是输出序列里面的个数
  28. while(!s.empty())
  29. {
  30. if((s.size())>1)
  31. flag=1;//不确定
  32. int first=s.top();
  33. s.pop();
  34. output[cnt++]=first+'A';//放入输出序列里面
  35. for(int i=0;i<n;i++)
  36. if(graph[first][i])//与入度为0的元素相连的元素
  37. {
  38. in[i]--;
  39. if(in[i]==0)
  40. s.push(i);//入栈
  41. }
  42. }
  43. if(cnt!=n)//假设没有环的话,序列里面的元素个数肯定等于输入的元素个数,就算在某个元素未输入之前,它的入度也初始化为0
  44. return 2;//有环
  45. else if(flag==1)//不确定的拓扑排序
  46. return -1;
  47. return 1;
  48. }
  49.  
  50. int main()
  51. {
  52. int n,m;
  53. while(cin>>n>>m&&(n||m))
  54. {
  55. ok=0;dilemma=0;no=0;
  56. memset(indegree,0,sizeof(indegree));
  57. memset(graph,0,sizeof(graph));
  58. for(int i=1;i<=m;i++)
  59. {
  60. cin>>c1>>c>>c2;//当出现矛盾或者通过一些条件可被确定序列,剩下的输入条件就不须要再处理了
  61. if(!ok&&!dilemma)//没有环,没有确定的拓扑排序,这里的拓扑排序必须输入的字母都有。比方输入ABCD 那么仅仅有AB不是确定的
  62. {
  63. int t1=c1-'A';
  64. int t2=c2-'A';
  65. if(graph[t2][t1])//双向边,有环,出现矛盾
  66. {
  67. cout<<"Inconsistency found after "<<i<<" relations."<<endl;
  68. dilemma=1;//出现矛盾
  69. continue;
  70. }
  71.  
  72. if(!graph[t1][t2])
  73. {
  74. graph[t1][t2]=1;
  75. indegree[t2]++;//入度++
  76. }
  77. int ans=topo(n);//确定返回1,有环返回2
  78. if(ans==2)
  79. {
  80. cout<<"Inconsistency found after "<<i<<" relations."<<endl;
  81. dilemma=1;
  82. continue;
  83. }
  84. if(ans==1)
  85. {
  86. cout<<"Sorted sequence determined after "<<i<<" relations: ";
  87. for(int k=0;k<n;k++)
  88. cout<<output[k];
  89. cout<<"."<<endl;
  90. ok=1;
  91. }
  92. }
  93. }
  94. if(!ok&&!dilemma)
  95. cout<<"Sorted sequence cannot be determined."<<endl;
  96. }
  97. return 0;
  98. }

[ACM] POJ 1094 Sorting It All Out (拓扑排序)的更多相关文章

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

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

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

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

  3. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

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

  4. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

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

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  7. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

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

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

  9. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

随机推荐

  1. 很吊炸天的Xcode插件,你想要的这都有

    整理自BigPolarBear的博客 .杂技杂记  以及CC此前整理. 古人云"工欲善其事必先利其器",打造一个强大的开发环境.是马上提升自身战斗力的绝佳途径!下面是搜集的一些有力 ...

  2. angular 设置全局常量

    一:在项目核心文件core.module.ts中设置全局静态常量 解释:相当于自动注入到inject中. providers:[ { provide:'BASE_CONFIG', useValue:' ...

  3. django-rest-framework框架 第二篇 之Mixin扩展类

    Mixin扩展类     ['列表操作','过滤','搜索','排序'] <一>:<1>创建项目: 配置 urls 主路由    配置model文件(举个例子,就以book为模 ...

  4. 关于mysql事务行锁for update实现写锁的功能

    关于mysql事务行锁for update实现写锁的功能 读后感:用切面编程的理论来讲,数据库的锁对于业务来说是透明的.spring的事务管理代码,业务逻辑代码,表锁,应该是三个不同的设计层面. 在电 ...

  5. Android ServiceManager启动

    许久就想写篇关于servicemanager的文章,之前对服务启动顺序诸如zygote,systemserver.等启动顺序理解有点混乱,现做例如以下理解分析: 事实上init进程启动后,Servic ...

  6. error app/styles/components/iconfont.scss (Line 12: Invalid GBK character "\xE5")

    因为要用到iconfont,引入iconfont到sass文件后,出现编译sass文件错误,如下截图: 解决方法:在顶部设置编码格式 @charset "utf-8"; 编译成功!

  7. 自己定义Dialog的具体步骤(实现自己定义样式一般原理)

    转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自己定义Dialog的具体步骤(实现自己定义样式一般原理)    ...

  8. IOS越狱开发环境搭建

    1:安装 mac ports 2:安装DPKG, 在终端输入sudo port -f install dpkg即可安装 3:安装theos Theos是一个基于Make的编译环境,我们正是用它来编译生 ...

  9. 【数学】概念的理解 —— 有序对(ordered pair)

    有序对 (a,b) 是一组对象,不同的顺序意味着不同的对象,(a,b)≠(b,a) 除非 a=b,正是因为对象的相对顺序是有着不同含义的,因此有时也称之为 2 维向量.与之相对的无序对(unorder ...

  10. php实现矩形覆盖

    php实现矩形覆盖 一.总结 很简单的斐波那契数列 二.php实现矩形覆盖 题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总 ...