POJ 1094 Sorting It All Out 拓扑排序+Floyd算法

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24697 Accepted: 8557

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.

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个字母,再给出字母之间大小关系,然后如果可以从大到小排序,则输出整体排序,如果对应关系矛盾,输出有矛盾,如果有不止一个对应关系,则输出存在多组对应关系,这道题其实运用拓扑算法,并且需要对拓扑算法进行一定修改,并且运用floyd看题目中是否有矛盾,判定方法就是求出全源对应关系,之后如果相同节点有大小对应关系,则证明题目存在矛盾,之后对数列带入拓扑算法,判断当寻找父节点为0的根节点时,发现有多个根节点,则证明对应关系不止一种,直接跳出函数继续更新路径后重新带拓扑排序,如果没有再发现根节点但是已排序节点数小于总结点数,则证明存在环路,即有矛盾,输出存在矛盾,之后的拓扑模板则不需要继续修改了,直接得到拓扑序列,并且输出即可AC,这道题其实也是道难度相对较大的题,各种关系量要注意分析的步骤,否则很容易因为粗心造成一定麻烦。


下面是AC代码:


#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int G[30][30],ans[30],degree[30],into[30],visit[30];
int Floyd_Washall(int n)
{
	int i,j,k;
    for (k=0;k<n;k++)
        for (i=0;i<n;i++)
            for (j=0;j<n;j++)
                if (G[i][j]==0&&G[i][k]==1&&G[k][j]==1)
                      G[i][j]=1;
	for(i=1;i<=n;i++)
		if(G[i][i]==1)
			return 1;
	return 0;
}
int Toplogical_sort(int n)
{   
     int i,j,top,count;
     bool p=true;
     top=0;
	 memset(visit,0,sizeof(visit));
	 memset(degree,0,sizeof(degree));
	 memset(ans,0,sizeof(ans));
	 for(i=0;i<n;i++)
		 for(j=0;j<n;j++)
			 if(G[i][j]==1)
				 degree[j]++;
	 for(i=0;i<n;i++)
	 {
		 count=0;
		 for(j=0;j<n;j++)
			 if(degree[j]==0&&visit[j]==0)
			 {
				 count++;
				 top=j;
			 }
		 if(count>=2)
			 return 0;
		 else if(count==0)
			 return 1;
		 ans[i]=top;
		 visit[top]=1;
		 for(j=0;j<n;j++)
			 degree[j]--;
	 }
	 return 2;
} 
int main()
{
	int n,m,i,a,b,flag;
	char str[400][5];
	while(1)
	{
		flag=0;
		memset(G,0,sizeof(G));
		cin>>n>>m;
		if(n==0&&m==0)
			break;
		for(i=0;i<m;i++)
			scanf("%s",str[i]);
		for(i=0;i<m;i++)
		{
			a=str[i][0]-'A';
			b=str[i][2]-'A';
			G[a][b]=1;
			flag=Floyd_Washall(n);
			if(flag==1)
				break;

			flag=Toplogical_sort(n);
			if(flag==2)
				break;
		}
		if(flag==1)
			printf("Inconsistency found after %d relations.\n",i+1);
		else if(flag==0)
			printf("Sorted sequence cannot be determined.\n");
		else
		{
			printf("Sorted sequence determined after %d relations: ",i+1);
			for(i=0;i<n;i++)
				printf("%c",ans[i]+'A');
			printf(".\n");
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值