nyoj 349 Sorting It All Out(拓扑排序度的理解)

Sorting It All Out

时间限制:
3000 ms  |  内存限制:
65535 KB 难度:
3

描述
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. 

样例输入
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.
来源
POJ
上传者

陈玉

输入n和m,n表示26个字母前n个字母,m表示有多少个关系,然后输入m个关系,判断是否这n个字母存在一个排序关系
如果存在输出在几个关系之后就输出几个关系之后就可以确定,比如第一个测试数据,前四个关系输入之后,就输出结果
后两个关系输入不用管,,如果存在环那么就输出冲突,如果不能确定次序就输出不能确定。
拓扑排序的理解,度的判断 

#include<stdio.h>
#include<string.h>
#define M 30
int re[M],path[M][M],du[M],d[M];
char str[8];
int topo(char s[],int n){
	int i,j,k;
	int A=s[0]-'A';
	int B=s[2]-'A';
	if(path[A][B]==0){
		path[A][B]=1;
		d[B]++;//因为每一次调用函数的时候要保持原来的图的数据,所以用d数组保存度不变 
	}
	for(i=0;i<n;i++)
		du[i]=d[i];//du数组来判断当前函数度的变化 
	int flag=1,cnt,pos=0;
	for(i=0;i<n;i++){
		cnt=0;
		for(j=0;j<n;j++){
			if(du[j]==0){
				cnt++;
				k=j;
			}
		}
		if(cnt==0) return -1;//cnt等于0表示存在环,即存在冲突 
		else if(cnt>1)	flag=0;//存在多个度为0的点,即次序不能确定 
		du[k]--;
		re[pos++]=k;
		for(j=0;j<n;j++){
			if(path[k][j]==1)
				du[j]--;
		}
	}
	if(flag) return 1;
	return 0; 
}
void result(int n,int m){
	int i,j;
	int ok=1;
	for(i=0;i<m;i++){
		scanf("%s",str);
		if(ok){
			int t=topo(str,n);
			if(t==1){
				printf("Sorted sequence determined after %d relations: ",i+1);
				for(j=0;j<n;j++)
					printf("%c",char(re[j]+'A'));
				printf(".\n");
				ok=0;
			}
			else if(t==-1){
				printf("Inconsistency found after %d relations.\n",i+1);
				ok=0;
			}
		}
	}
	if(ok)	printf("Sorted sequence cannot be determined.\n");
}
int  main(){
	int n,m;
	char str[8];
	while(scanf("%d%d",&n,&m),n+m){
		if(m<n-1){//关系不够不能确定次序 
			for(int i=0;i<m;i++)
				scanf("%s",str);
			printf("Sorted sequence cannot be determined.\n");
		}else{
			memset(d,0,sizeof(d));
			memset(path,0,sizeof(path));
			result(n,m);
		}
	}
	return 0;
}
    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/ling_du/article/details/48577777
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞