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
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.
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 0Sample Output
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
题意:看第几行边就能看出有唯一的顺序或者 成环 ,如果所有边加完后都没有唯一顺序也没成环,那么输出
Sorted sequence cannot be determined.
解:每加新的边,都进行1次拓扑排序
#include<stdio.h> #include<string> #include<string.h> #include<iostream> #include<math.h> #include<queue> #include<vector> #include<algorithm> #include<map>d #define inf 0x3f3f3f3f #define ll long long #define maxx 5000005 using namespace std; int n,m,tot,flag,cnt,flag2; struct node { int en,next; }e[10000]; int p[10000]; int a[10000]; int b[10000]; int vis[10000]; int ans[10000]; void add(int u,int v) { e[tot].en=v; e[tot].next=p[u]; p[u]=tot++; } void aa(int u) { int kk=0; int pp=0; queue<int>q; for(int i=0;i<n;i++) if(vis[i]&&!b[i]) q.push(i); while(!q.empty()) { int x=q.front(); q.pop(); if(!q.empty()) kk=1; ans[pp++]=x; for(int i=p[x];i+1;i=e[i].next) { int y=e[i].en; b[y]--; if(!b[y]) q.push(y); } } if(pp!=cnt) flag=u; if(!kk&&pp==n) flag2=u; } int main() { while(~scanf("%d%d",&n,&m)&&n+m) { char s[10]; memset(p,-1,sizeof(p)); memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); tot=0; cnt=0; flag=0; flag2=0; for(int i=1;i<=m;i++) { scanf("%s",s); if(flag) continue; if(flag2) continue; int u=s[0]-'A'; int v=s[2]-'A'; if(!vis[u]) { vis[u]=1; cnt++; } if(!vis[v]) { vis[v]=1; cnt++; } add(u,v); a[v]++; memcpy(b,a,sizeof(a)); aa(i); } if(flag) { printf("Inconsistency found after %d relations.\n",flag); continue; } if(flag2) { printf("Sorted sequence determined after %d relations: ",flag2); for(int i=0;i<n;i++) printf("%c",ans[i]+'A'); printf(".\n"); continue; } printf("Sorted sequence cannot be determined.\n"); } }