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.
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.
思路:拓扑排序题,这个说实话我觉得很难啊。因为这个题要输出第几个输入数据出现问题,逻辑上崩了,还是要多练...
主要有两点:
一个是判断环,就是每次加边时,进行一次拓扑排序操作,如果进入队列的点的总和小于已经输入的点则一定是有环的。
另一个是判断是否确定了唯一排名。如果同一时刻队列里有多于1个的值,则说明上个节点有多个后驱排名相同,就没有唯一排名了。
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<vector> 5 #define N 30 6 using namespace std; 7 int cnt,n,m; 8 int mark[N],d[N],dc[N],ans[N]; 9 vector<int> G[N]; 10 11 int TPsort(){ 12 memcpy(dc,d,sizeof(d)); 13 queue<int> q; 14 for(int i=0;i<n;i++) 15 if(dc[i]==0) q.push(i); 16 int flag=0; 17 cnt=0; 18 while(!q.empty()){ 19 if(q.size()>1) flag=1; 20 int p=q.front(); 21 q.pop(); 22 ans[cnt++]=p; 23 for(int i=0;i<G[p].size();i++){ 24 int v=G[p][i]; 25 dc[v]--; 26 if(dc[v]==0) q.push(v); 27 } 28 } 29 if(cnt!=n) return -1; 30 else if(flag!=0) return 0; 31 else return 1; 32 33 } 34 35 int main(){ 36 while(scanf("%d%d",&n,&m)){ 37 if(n==0&&m==0) break; 38 39 for(int i=0;i<n;i++) 40 G[i].clear(); 41 memset(mark,0,sizeof(mark)); 42 memset(d,0,sizeof(d)); 43 int a,b,r,id,flag=0; 44 char s[5]; 45 46 for(int k=1;k<=m;k++){ 47 scanf("%s",s); 48 a=s[0]-'A'; 49 b=s[2]-'A'; 50 G[a].push_back(b); 51 d[b]++; 52 53 if(flag) continue; 54 55 r=TPsort(); 56 if(r==1||r==-1) flag=1,id=k; 57 58 } 59 60 if(r==1){ 61 printf("Sorted sequence determined after %d relations: ",id); 62 for(int i=0;i<n;i++) 63 printf("%c",ans[i]+'A'); 64 printf(". "); 65 flag=1; 66 } 67 else if(r==-1){ 68 printf("Inconsistency found after %d relations. ",id); 69 flag=1; 70 } 71 else if(r==0){ 72 printf("Sorted sequence cannot be determined. "); 73 } 74 } 75 76 return 0; 77 }