nyoj349 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
上传者
陈玉

刚开始没有理解题意 。。想了好两三天 哈哈(好吧 是我懒了)

拓扑排序过程:

1先建图,然后寻找入读为0的顶点

2:然后找到与这个顶点有关的边和点,删去边,点的入度-1

3.继续寻找下一个入度为0的顶点。。重复2.

总共三种情况 冲突 可以排序 信息不完整

判断冲突 起初我想错了 总想着并查集判断成环冲突 可以却发现我错的大大的 因为并查集判断的是集合  也就是无向的

而这道题是有向的。

比如A<B A<C B<C用并查集判断是一个集合 是不行的。所以还是用我们的强大的深搜判断有向环


其中判断冲突 实在输入的时候就判断的 可以排序和信息不完整在输入数据完毕后再检查。

拓扑排序处理信息不完整和冲突

1.如果出现两个同时入度为0的,并且根是自己就是信息不完整。

2.如果栈的大小刚好等于比较的数的个数 那么就是能够排序 如果栈的大小小于比较的数的个数 就是信息不完整

用的vector容器模拟邻接表。queue实现拓扑排序,stack存贮大小

#include <stdio.h>
#include <vector>
#include <string.h>
#include <queue>
#include <stack>
using namespace std;
vector<int>map[27];//邻接表 
int degree_in[27];//顶点的入度 
bool have[27],vis[27];//hava判断是否是比较的数 ,vis是在判断成环的时候 判断是否已经遍历 
stack<int>s; 
//拓扑排序 
int top_sort(int n)
{
	queue<int>temp;
	int pos;
	while(!s.empty())
	s.pop();
	while(!temp.empty())
	temp.pop(); 
	for(int i=0;i<26;i++)
	{
		if(degree_in[i]==0&&have[i])
		{
			temp.push(i);
			have[i]=false;
		}
	}
	while(!temp.empty())
	{
		if(temp.size()>1)
		return 1;
		pos=temp.front();
		temp.pop();
		s.push(pos);
		for(int i=0;i<map[pos].size();i++)
		{
			if(--degree_in[map[pos][i]]==0)
			temp.push(map[pos][i]); 
		}
	}
	if(s.size()==n)
	return 2;
	else 
	return 1;
}
//判断是否成有向环 
bool judge(int x)
{
	for(int i=0;i<map[x].size();i++)
	{
		if(vis[map[x][i]])
		return false;
		vis[map[x][i]]=true;
		if(judge(map[x][i]))
		vis[map[x][i]]=false;
		else
		return false;
	}
	return true;
}
int main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		if(!m&&!n)
		break;
		memset(degree_in,0,sizeof(degree_in));
		memset(have,false,sizeof(have));
		memset(map,0,sizeof(map));
		int mark=-1;
		for(int i=0;i<m;i++)
		{
			char ch1,ch2;
			getchar();
			scanf("%c<%c",&ch1,&ch2);
			int a=ch1-'A';
			int b=ch2-'A';
			map[b].push_back(a);
			degree_in[a]++;
			have[a]=have[b]=true;  
			if(mark==-1)
			{
				memset(vis,false,sizeof(vis));
				vis[b]=true;
				if(!judge(b))
				{
					mark=i;
				}     
			}
		}
		if(mark!=-1)
		{
			printf("Inconsistency found after %d relations.\n",mark+1);
			continue;
		}
		else
		{
			int x;
			x=top_sort(n);
			if(x==1)
			printf("Sorted sequence cannot be determined.\n");
			if(x==2)
			{
				printf("Sorted sequence determined after %d relations: ",n);
				while(!s.empty())
				{
					printf("%c",s.top()+'A');
					s.pop();
				}
				printf(".\n");
			}
		}
	}
	return 0;
}





你可能感兴趣的:(nyoj349 Sorting It All Out(拓扑排序))