poj--1094

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 30798 Accepted: 10677

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种关系可以确定使,就只输入不做处理就行了.wa了好几次,终于ac了大笑

提供几组测试数据:

6 5
> A<D
> B<C
> A<B
> D<E
> C<D
> Sorted sequence cannot be determined.
> 
> 6 6
> A<D
> B<C
> E<F
> B<A
> D<E
> A<B
> Inconsistency found after 6 relations.
> 
> 6 6
> D<E
> B<F
> A<D
> F<C
> A<F
> E<B
> Sorted sequence determined after 6 relations: ADEBFC.
> 
> 3 3
> A<B
> B<C
> C<A
> Sorted sequence determined after 2 relations: ABC.
> 
> 4 6
> A<B
> A<C
> B<C
> C<D
> B<D
> A<B
> Sorted sequence determined after 4 relations: ABCD.
> 
> 3 2
> A<B
> B<A
> Inconsistency found after 2 relations.
> 
> 2 2
> A<B
> B<A
> Sorted sequence determined after 1 relations: AB.
> 
> 26 1
> A<Z
> Sorted sequence cannot be determined.


代码如下:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
int count[30];
int temp[30];
char relation[3],seq[30];
bool alpha[26];
vector<vector<char> > v;//这样定义需要重置大小 
//vector<char> v[10];
int n,m;
char ss;
int  topo(int s)
{
	int k,flag,i,g,che;
	bool sign,esign;
	int sum[30];
	sign= false;
	esign=false;
	flag=0;g=0;
	for(i=0;i<n;i++) 
	temp[i]=count[i];//复制 
    while(1)
    {
       che=0;k=-1;
       for(i=0;i<n;i++)
       { 
     	  if(temp[i]==0&&alpha[i])
    	  {
    	        k=i;
    	    	che++;
    	        //temp[i]=-1;
			    //flag++;	
    	  }
       }
       if(k!=-1)
       {
       	temp[k]=-1;
       	flag++;
       }
	   if(che>1)//出现多个度为零的点 
       {
       	esign=true;
       }
       else if(che==0&&flag<s)//出现环 
       {
       	sign=true;
       	break;
       }
       else if(!esign)
       {
       	seq[g]='A'+k;
       	g++;
       }
       if(flag==s)
       break;
	   for(i=0;i<v[k].size();i++)
	    {
		   temp[v[k][i]]--;
	    }
    }
    ss=seq[0];
    if(sign)//出现环 
    return -1;
    else if(esign)//还无法确定关系 
    return 0;
    else if(flag==n)//可以得出关系 
    return n;
    else return 0;
    
}
int main()
{
	int i,j,t,k,c;
	int determind;
	while(cin>>n>>m)
	{
		if(n==0&&m==0)break; 
		memset(count,0,sizeof(count));//入边 
		memset(alpha,false,sizeof(alpha));//是否输入过,用于统计输入的结点数 
		memset(seq,0,sizeof(seq));
		v.clear();v.resize(n); //重置大小 
		c=0;determind=0;
		for(i=0;i<m;i++)
		{
			cin>>relation;
			count[relation[2]-'A']++;
			v[ relation[0]-'A'].push_back(relation[2]-'A');
			if(!alpha[relation[0]-'A'])
			{
				c++;alpha[relation[0]-'A']=true;
			}
			if(!alpha[relation[2]-'A'])
			{
			    c++;alpha[relation[2]-'A']=true;
			}
			if(determind==0)//没有得到1,2两种情况 
			{
				t=topo(c);
				if(t==-1)
				{
					determind=-1;
					k=i+1;
				} 
				else if(t==n)
				{
					determind=1;
					k=i+1;
				}
			}
		}
		seq[0]=ss;
		if(determind==-1)
		cout<<"Inconsistency found after "<< k <<" relations.\n"; 
		else if(determind==0)
		cout<<"Sorted sequence cannot be determined.\n";
		else
		cout<<"Sorted sequence determined after "<< k << " relations: "<< seq <<".\n";
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值