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.

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 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)