每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为0的点,如果出现了就说明不能确定大小关系。不论出不出现多个入度为0的点拓扑排序都要进行到最后来判断是否出现环,因为一旦出现环就不用再比了,一定是不能排序的。
另外,注意输出序列之后的那个逗号。。。。。。
Sorting It All Out
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 13
Problem 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.
Source
PKU
#include <iostream> #include <cstring> #include <stdio.h> #define inf 0xfffffff #define maxn 27 using namespace std; int n,m; char les,first,second; int mp[maxn][maxn],ans[maxn]; int topo() { int i,j,res=0; int into[maxn]={0}; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(mp[i][j]>0) into[j]++; } } for(i=0;i<n;i++) { int cnt=0,tmp=-1; for(j=0;j<n;j++) { if(into[j]==0) { cnt++; tmp=j; } } if(cnt==0) return 1; if(cnt>1) res=2; ans[i]=tmp; into[tmp]=-1; for(j=0;j<n;j++) { if(mp[tmp][j]>0) into[j]--; } } if(res==2) return 2; else return 3; } int main() { // freopen("in.txt","r",stdin); while(cin>>n>>m&&(n+m)) { memset(mp,0,sizeof(mp)); memset(ans,0,sizeof(ans)); int i,tp=2,last=0,num=0; for(i=0;i<m;i++) { cin>>first>>les>>second; int a=first-'A',b=second-'A'; mp[a][b]=1; if(tp==2) { tp=topo(); } if(last==2&&(tp==1||tp==3)) { num=i+1; } last=tp; } if(tp==3) { cout<<"Sorted sequence determined after "<<num<<" relations: "; for(i=0;i<n;i++) cout<<char(ans[i]+'A'); cout<<'.'; cout<<endl; } else if(tp==2) cout<<"Sorted sequence cannot be determined."<<endl; else if(tp==1) cout<<"Inconsistency found after "<<num<<" relations."<<endl; } return 0; }