Sorting It All Out 拓扑排序&&判断是否存在环,是否关系包含所有的点
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.
**************************************************************************************************************************经典的拓扑排序
***************************************************************************************************************************
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 int a[26][26]; 8 queue<char>Q1;//记录两者之间的关系 9 int de[26],de2[26]; 10 int relations; 11 bool flag; 12 int kind,it,jt,kt,n,m; 13 void carry(int n,int ret) 14 { 15 int i,j,sum=0; 16 int num; 17 bool sign=true; 18 queue<int>Q; 19 for(i=0;i<n;i++) 20 { 21 if(de[i]==0) 22 Q.push(i); 23 } 24 while(!Q.empty()) 25 { 26 num=0; 27 for(i=0;i<n;i++) 28 { 29 if(de[i]==0) 30 num++; 31 } 32 if(num>1) 33 sign=false;//存在不确定关系 34 i=Q.front(); 35 Q.pop(); 36 Q1.push(i+'A');// 37 sum++; 38 de[i]=-1; 39 for(j=0;j<n;j++) 40 { 41 if(a[i][j]==true) 42 { 43 de[j]--; 44 if(de[j]==0) 45 Q.push(j); 46 } 47 } 48 49 50 } 51 //如果存在确定关系,记录位置,不再往下算 52 if(sum==n&&sign) 53 { 54 flag=true; 55 kind=1; 56 relations=ret; 57 } 58 else 59 for(i=0;i<n;i++) 60 if(de[i]>0)//如果存在环,不再往下算 61 { 62 flag=true; 63 kind=2; 64 relations=ret; 65 break; 66 } 67 if(!flag)//如果都不满足,恢复 68 { 69 for(i=0;i<n;i++) 70 de[i]=de2[i]; 71 } 72 return; 73 74 } 75 int main() 76 { 77 char left,mid,right; 78 int ni,nj; 79 while(scanf("%d%d",&n,&m)) 80 { 81 getchar(); 82 flag=false; 83 kind=0; 84 relations=0; 85 if(n==0&&m==0) 86 break; 87 for(it=0;it<n;it++)//初始化 88 { 89 for(jt=0;jt<n;jt++) 90 a[it][jt]=false; 91 de[it]=-1; 92 de2[it]=-1; 93 } 94 for(it=0;it<m;it++) 95 { 96 cin>>left>>mid>>right; 97 getchar(); 98 ni=left-'A';//两点有联系时,入度先都置0 99 nj=right-'A'; 100 if(de[ni]==-1) 101 { 102 de[ni]=0; 103 de2[ni]=0; 104 } 105 if(de[nj]==-1) 106 { 107 de[nj]=0; 108 de2[nj]=0; 109 } 110 if(!a[ni][nj]) 111 { 112 a[ni][nj]=true;//ni和nj有关系 113 de[nj]++; 114 de2[nj]++; 115 } 116 if(!flag) 117 { 118 while(!Q1.empty())//没找到确定关系时,要一直恢复 119 Q1.pop(); 120 carry(n,it+1); 121 } 122 } 123 if(flag) 124 { 125 if(kind==1) 126 { 127 printf("Sorted sequence determined after %d relations: ",relations); 128 while(!Q1.empty()) 129 { 130 cout<<Q1.front(); 131 Q1.pop(); 132 } 133 cout<<"."<<endl; 134 } 135 else 136 if(kind==2) 137 { 138 printf("Inconsistency found after %d relations.\n",relations); 139 } 140 } 141 else 142 printf("Sorted sequence cannot be determined.\n"); 143 144 } 145 }