1. 程式人生 > >Sorting It All Out (拓撲排序)(能否確定字母排序)

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

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

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.

題意:對於N個大寫字母,給定它們之間的關係,要求判斷下列三種情況
         1.到第k次就能確定有環  
         2.到第k次就能確定字母間關係
        3.直到判斷完所有輸入的字母關係,還是無法確定字母間關係 

 思路:每次輸入都要進行拓撲排序來判斷上述三種情況

AC程式碼:

//採用臨接表儲存字母關係
/*三種情況
 1.到第k次就能確定有環  
 2.到第k次就能確定字母間關係
 3.直到所以字母關係輸盡,還是無法確定字母間關係 
 */ 
#include<iostream> 
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N=30;
vector<int> G[N];
int n,m;
int deg[N],vis[N];
int d[N],ans[N];
//拓撲排序模板 
int Tsort()
{
	for(int i=0;i<n;i++)//新開一個數組存當前度的情況,如果用原來的deg[]則會改變度的狀態
	   d[i]=deg[i];
	queue<int> q;
	for(int i=0;i<n;i++)
	    if(!d[i]) q.push(i);
	int cnt=0,flag=0;
	while(!q.empty())
	{
		if(q.size()>1) flag=1;//如果度為0的字母個數不為1,則是說明n個字母的關係處於不確定狀態(如果能確定字母順序,q.size()==1) 
		int p=q.front();
		q.pop();
		ans[cnt++]=p;//cnt統計度為0的字母個數,用於判斷是否有環,如果cnt!=n,則說明有環 
		for(int i=0;i<G[p].size();i++)
		{
			int v=G[p][i];
			d[v]--;
			if(d[v]==0)
			   q.push(v);
		} 
	}
	if(cnt!=n) return -1;
	else if(flag) return 0;
	else return 1;
}
int main()
{
	while(cin>>n>>m)
	{
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++)
		   G[i].clear();
		memset(vis,0,sizeof(vis));
		memset(deg,0,sizeof(deg));
	    char c[5];
		int a,b,r,id,flag=0;
		for(int i=1;i<=m;i++)
		{
		    scanf("%s",c);
			a=c[0]-'A';
			b=c[2]-'A';
			G[a].push_back(b);
			deg[b]++;
			
			if(flag) continue;//flag==1,說明字母順序確定或者有環 
			
			r=Tsort();
			if(r==1||r==-1) 
			{
				flag=1;
				id=i;
			}
		}
		if(r==1)//到第k次就能確定字母間關係
		{
			printf("Sorted sequence determined after %d relations: ",id);
			for(int i=0;i<n;i++)
			    printf("%c",ans[i]+'A');
			printf(".\n");
			flag=1; 
		}
		else if(r==-1)//到第k次就能確定有環  
		{
			printf("Inconsistency found after %d relations.\n",id);
			flag=1;
		}
		else if(r==0)//直到所以字母關係輸盡,還是無法確定字母間關係  
		{
			printf("Sorted sequence cannot be determined.\n");
		}
	}
	return 0;
}