Sorting It All Out
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 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.
输出
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.
样例输入
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
样例输出
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
#include <bits/stdc++.h> #define maxn 10005 using namespace std; typedef long long ll; struct Edge { int v,next; }; struct M { Edge edge[maxn]; int head[maxn]; int cnt; void init() { memset(head,-1, sizeof(head)); cnt=0; } void addedge(int u,int v) { edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } }Mp; int ind[30],n,in[30]; int Low_ans[maxn]; int Upper_ans[maxn]; set<char> se; bool Low_Topsort() { int tot=-1; priority_queue<int,vector<int>,greater<int> > q; for(int i=0;i<26;i++) { if(ind[i]==0&&se.count(i+'A')) q.push(i); } while(!q.empty()) { int u=q.top(); q.pop(); Low_ans[++tot]=u; for(int i=Mp.head[u];i!=-1;i=Mp.edge[i].next) { int v=Mp.edge[i].v; ind[v]--; if(ind[v]==0) q.push(v); } } if(tot==se.size()-1) return true; else return false; } void Upper_Topsort() { int tot=-1; priority_queue<int> q; for(int i=0;i<26;i++) { if(ind[i]==0&&se.count(i+'A')) q.push(i); } while(!q.empty()) { int u=q.top(); q.pop(); Upper_ans[++tot]=u; for(int i=Mp.head[u];i!=-1;i=Mp.edge[i].next) { int v=Mp.edge[i].v; ind[v]--; if(ind[v]==0) q.push(v); } } } int main() { int m,i; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; se.clear(); Mp.init(); int f=0; int cnt=0; int ans[maxn]={0}; memset(in,0, sizeof(in)); for(int i=1;i<=m;i++) { string s; memset(Low_ans,0, sizeof(Low_ans)); memset(Upper_ans,0, sizeof(Upper_ans)); cin>>s; se.insert(s[0]); se.insert(s[2]); Mp.addedge(s[0]-'A',s[2]-'A'); in[s[2]-'A']++; for(int i=0;i<26;i++) { ind[i]=in[i]; } if(!Low_Topsort()&&!f) { f=1; cnt=i; } else { for(int i=0;i<26;i++) { ind[i]=in[i]; } Upper_Topsort(); //printf("\n"); bool flag=true; for(int i=0;i<se.size();i++) { if(Low_ans[i]!=Upper_ans[i]) { flag=false; break; } } if(flag&&!f&&se.size()==n) { f=2; cnt=i; for(int i=0;i<se.size();i++) { ans[i]=Low_ans[i]; } } } } if(!f) { cout<<"Sorted sequence cannot be determined."<<endl; } else if(f==1) { printf("Inconsistency found after %d relations.\n",cnt); } else if(f==2) { printf("Sorted sequence determined after %d relations: ",cnt); for(int i=0;i<se.size();i++) { printf("%c",ans[i]+'A'); } printf("."); printf("\n"); } } return 0; }