POJ 1094 Sorting It All Out
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23404 | Accepted: 8093 |
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.
经典的拓扑排序题,重点部分请参见代码注释
[C++]
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<queue> 5 6 using namespace std; 7 8 int n,m; 9 char sorted[26]; 10 queue<char> Q; 11 vector<char> v[26]; 12 13 //拓扑排序函数,如果能确定唯一的拓扑排序,函数返回1;如果图中有环,函数返回-1;如果能进行拓扑排序但不唯一,或者并没有用上n个点,函数返回0 14 int toposort() 15 { 16 int du[26]; 17 //用mentionednum记录当前图中用到了多少个点 18 int mentionednum=0; 19 //mentioned[]用来记录改字母是否在图中出现过,maybecontinue记录是否是否能给出唯一的拓扑排序,即在每次循环时,只能有一个入度为0的点,否则就需要要再加条件,将maybecontinue改为true 20 bool visited[26],mentioned[26],maybecontinue=false; 21 memset(du,0,sizeof(du)); 22 memset(sorted,0,sizeof(sorted)); 23 memset(visited,false,sizeof(visited)); 24 memset(mentioned,false,sizeof(mentioned)); 25 for(int i=0;i<n;i++) 26 { 27 if(v[i].size()) 28 mentioned[i]=true; 29 for(int j=0;j<v[i].size();j++) 30 { 31 mentioned[v[i][j]-'A']=true; 32 du[v[i][j]-'A']++; 33 } 34 } 35 for(int i=0;i<n;i++) 36 if(mentioned[i]) 37 mentionednum++; 38 int times=0; 39 for(int i=0;i<n;i++) 40 if((!du[i])&&mentioned[i]) 41 { 42 Q.push(i+'A'); 43 visited[i]=true; 44 times++; 45 } 46 if(Q.empty()) 47 return -1; 48 if(times>1) 49 maybecontinue=true; 50 int tot=0; 51 while(!Q.empty()) 52 { 53 sorted[tot++]=Q.front(); 54 Q.pop(); 55 times=0; 56 for(int i=0,t=v[sorted[tot-1]-'A'].size();i<t;i++) 57 { 58 int x=sorted[tot-1]-'A'; 59 if(!visited[v[x][i]-'A']) 60 { 61 du[v[x][i]-'A']--; 62 if(!du[v[x][i]-'A']) 63 { 64 Q.push(v[x][i]); 65 visited[v[x][i]-'A']=true; 66 times++; 67 } 68 } 69 } 70 if(times>1) 71 maybecontinue=true; 72 } 73 //如果tot==mentionednum,则存在拓扑排序,否则判断出图中存在有向环,拓扑排序失败,函数返回-1 74 if(tot==mentionednum) 75 { 76 if(mentionednum==n&&(!maybecontinue)) //如果mentionednum和n相等且拓扑排序是唯一的,说明所有的点都已经拓扑排序好了,函数返回1,否则还需要再添加条件,函数返回0 77 return 1; 78 return 0; 79 } 80 return -1; 81 } 82 83 int main() 84 { 85 while(cin>>n>>m) 86 { 87 if(n==0&&m==0) 88 break; 89 int i; 90 string temp; 91 for(i=0;i<26;i++) 92 v[i].clear(); 93 for(i=1;i<=m;i++) 94 { 95 cin>>temp; 96 v[temp[0]-'A'].push_back(temp[2]); 97 int t=toposort(); 98 if(t) 99 { 100 if(t==1) 101 cout<<"Sorted sequence determined after "<<i<<" relations: "<<sorted<<"."<<endl; 102 else if(t==-1) 103 cout<<"Inconsistency found after "<<i<<" relations."<<endl; 104 break; 105 } 106 } 107 if(i>m) 108 cout<<"Sorted sequence cannot be determined."<<endl; 109 for(i++;i<=m;i++) 110 cin>>temp; 111 } 112 113 return 0; 114 }