poj--1094

原创 2015年11月19日 15:35:00
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;
}



相关文章推荐

poj 1094代码加测试工具

  • 2013年06月30日 13:42
  • 108KB
  • 下载

【POJ】1094 Sorting It All Out 拓扑排序

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions:...

poj-1094 Sorting It All Out[拓扑排序]

感觉需要注意的细节有点多。 (1)先判断是否有环  用used表示能拓扑的点 vis表示能搜索到的点 假如两者总数不等说明有环。 (2)再判断拓扑序是否唯一 假如一次寻找0入度节点有多个即不唯...

poj.1094

本题的意思是,给定一个n和m,其中n代表共有A、B、C等共n个大写字母,m代表有m个序列,题目要求是根据给出的序列判断是否能得出一个能确定的序列:下面是代码: #include #include ...

POJ1094(简单拓扑排序)

题目大意:给你的m种大小情况对n个字母进行拓扑排序,严格的说是能不能给出特定的顺序,这题主要是判断的先后顺序,先判断有没有环,如果有环的话先输出存在环,如果有确定结果的话输出结果,如果都没有的话输出不...

【基础拓扑排序】poj 1094

这题很基础也很经典,不过个人认为有一个非常容易出错的地方,就是矛盾Inconsistency的优先级比不确定的优先级高,因为当前有矛盾就立即跳出,而当前有多个点入度为0就不能立即输出,有可能后来加入的...
  • leolin_
  • leolin_
  • 2011年08月15日 22:59
  • 259

POJ - 1094 Sorting It All Out解题报告

Kahn····

POJ 1094 Sorting It All Out (拓扑排序)

题目链接 #include #include #include using namespace std; struct node { int in;//入度 bool valid...
  • szhhck
  • szhhck
  • 2012年08月12日 12:40
  • 450

POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

Description An ascending sorted sequence of distinct values is one in which some form of a less-than...

POJ1094 Sorting It All Out

题目来源:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory L...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj--1094
举报原因:
原因补充:

(最多只允许输入30个字)