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

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.
解题思路:拓扑排序裸题。做法:边读边进行拓扑排序,如果最后拓扑的个数小于n,说明图中有环,返回0,输出此时的位置i和对应的信息,下次直接跳过字符串的操作即可;如果返回值为1,说明拓扑次序唯一,直接输出对应信息和拓扑序列即可,同样下次直接跳过对字符串的操作;如果前两者都没有发生,即某次队列中顶点的入度数为0的个数不止一个,说明拓扑次序是不唯一的,输出对应的结果即可。
AC代码:
 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 #include<string.h>
 5 #include<map>
 6 #include<cstdio>
 7 using namespace std;
 8 const int maxn=26;
 9 int n,m,out[maxn],InDeg[maxn];char cs[5];
10 vector<int> vec[maxn];
11 queue<int> que;
12 int topsort(){//flag:-1次序不确定;1:次序唯一
13     while(!que.empty())que.pop();//清空
14     int cnt=0,flag=1,in[maxn];
15     for(int i=0;i<n;++i)in[i]=InDeg[i];//拷贝每个节点的入度数
16     for(int i=0;i<n;++i)
17         if(!in[i])que.push(i);//先将入度为0的进队
18     while(!que.empty()){
19         if(que.size()>1)flag=-1;//次序不确定
20         int now=que.front();out[cnt++]=now;que.pop();
21         for(size_t i=0;i<vec[now].size();++i)
22             if(--in[vec[now][i]]==0)que.push(vec[now][i]);
23     }
24     if(cnt!=n)return 0;//成环
25     return flag;
26 }
27 int main(){
28     while(cin>>n>>m&&(n+m)){
29         getchar();
30         memset(InDeg,0,sizeof(InDeg));
31         for(int i=0;i<n;++i)vec[i].clear();//全部清空
32         int sign=0;
33         for(int i=1;i<=m;++i){
34             cin>>cs;
35             if(sign)continue;//跳过下面的操作,表示结论已出
36             int f1=cs[0]-'A',f2=cs[2]-'A';
37             vec[f1].push_back(f2);
38             InDeg[f2]++;
39             int k=topsort();
40             if(k==0){//成环
41                 printf("Inconsistency found after %d relations.\n",i);
42                 sign=1;//同时标记为1
43             }
44             if(k==1){//有序
45                 printf("Sorted sequence determined after %d relations: ",i);
46                 for(int j=0;j<n;++j)printf("%c",out[j]+'A');
47                 printf(".\n");
48                 sign=1;
49             }
50         }
51         if(!sign)printf("Sorted sequence cannot be determined.\n");//最后才确定次序是否唯一
52     }
53     return 0;
54 }

posted @ 2018-07-16 01:10  霜雪千年  阅读(293)  评论(0编辑  收藏  举报