设为首页 加入收藏

TOP

[ACM] POJ 1094 Sorting It All Out (拓扑排序)
2015-07-20 18:06:40 来源: 作者: 【 】 浏览:13
Tags:ACM POJ 1094 Sorting All Out 拓扑 排序

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

4 6
A

Sample Output

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

Source

East Central North America 2001


解题思路:

拓扑排序的应用。参考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.如果在没有读完所有输入就能出答案,一定要把剩下的行都读完。

代码:

#include 
    
     
#include 
     
       #include 
      
        #include 
       
         #include 
        
          using namespace std; int indegree[30];//保存入度 int graph[30][30];//是否有边 char output[30];//输出可确定序列 bool ok;//可以被确定 bool dilemma;//有环,矛盾 bool no;//不能被确定 char c1,c,c2;//输入 int topo(int n) { int in[30]; for(int i=0;i
         
          s;//入度为0的点进栈 for(int i=0;i
          
           1) flag=1;//不确定 int first=s.top(); s.pop(); output[cnt++]=first+'A';//放入输出序列里面 for(int i=0;i
           
            >n>>m&&(n||m)) { ok=0;dilemma=0;no=0; memset(indegree,0,sizeof(indegree)); memset(graph,0,sizeof(graph)); for(int i=1;i<=m;i++) { cin>>c1>>c>>c2;//当出现矛盾或者通过一些条件可被确定序列,剩下的输入条件就不需要再处理了 if(!ok&&!dilemma)//没有环,没有确定的拓扑排序,这里的拓扑排序必须输入的字母都有。比如输入ABCD 那么只有AB不是确定的 { int t1=c1-'A'; int t2=c2-'A'; if(graph[t2][t1])//双向边,有环,出现矛盾 { cout<<"Inconsistency found after "<
            
             


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 4292Food(网络流之最大流) 下一篇HDU 1505 Largest Rectangle in a..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: