POJ-1094-Sorting It All Out

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33871 Accepted: 11858

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.

Source

East Central North America 2001

这道拓扑排序题有两点需要注意的是,它是没输入一对比较后都要判断一次它是否可以排序或则是否矛盾,如果找到一个矛盾的或则成立的拓扑序列,则对后续输入不作处理,如果到输入的末尾都没找到上述两种情况则输出Sorted sequence cannot be determined.
对于判断部分,要注意判断如果队列中如果元素大于1
就一定无序,因为有序是一对一的,每次度为一的点只可能有一个

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

int Map[30][30],in[30],in1[30],out[30],out1[30],vist[30];
int n,m,NUM1,NUM2,NUM3,num;
int flag1,flag2,flag3;
int str[30],cnt;

void pan(int Num)
{
	if(flag3 || flag1)//如果有序或冲突则不做后续判断
		return;
	cnt = 0;
	memset(str,0,sizeof(str));
	queue<int>q;
	flag2 = 0;
	memset(vist,0,sizeof(vist));

	for(int i = 1; i <= n; i++)
	{
		in1[i] = in[i];
		out1[i] = out[i];
		if(in1[i] == 0)
		{
			vist[i] = 1;
			q.push(i);
		}
	}

	int pos;
	while(!q.empty())
	{
		int Q = q.size();
		if(Q>1)
			flag2 = 1;
		pos = q.front();
		if(!flag1)
			str[cnt++] = pos;

		q.pop();
		for(int j = 1; j <= n; j++)
		{
			if(!vist[j] && Map[pos][j])
			{
				in1[j]--;
				out1[pos]--;
				if(in1[j] == 0)
				{
					vist[j] = 1;
					q.push(j);
				}
			}
		}
	}

	for(int i = 1; i <= n; i++)
	{
		if(in1[i])
		{
			flag3 = 1;
			NUM3 = Num;
			break;
		}
	}

	if(!flag2 && !flag3 && !flag1)
	{
		flag1 = 1;
		NUM1 = Num;
	}

}

int main()
{
	while(~scanf("%d %d%*c",&n,&m) && (n||m))
	{
		memset(Map,0,sizeof(Map));
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));

		num = 0;
		flag1 = flag3 = 0;
		while(m--)
		{
			num++;
			char A,B;
			scanf("%c%*c%c%*c",&A,&B);
			int x = A-'A'+1;
			int y = B-'A'+1;
			if(!Map[x][y])
			{
				Map[x][y] = 1;
				in[y]++;
				out[x]++;
			}
			pan(num);
		}
		if(flag3)
			cout<<"Inconsistency found after "<<NUM3<<" relations."<<endl;
		else if(flag1)
		{
			cout<<"Sorted sequence determined after "<<NUM1<<" relations: ";
			for(int i = 0; i < cnt; i++)
				printf("%c",'A'+str[i]-1);
			cout<<"."<<endl;
		}
		else
			cout<<"Sorted sequence cannot be determined."<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值