Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32801 | Accepted: 11394 |
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.
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.题意:1给出m条关系,如果可以确定一个唯一的关系,则把这个关系输出来
2如果在i条关系存在环,则输出在i条关系存在环
3其他情况输出不确定
每给出一条关系都需要判断一下,如果有1或2的情况,则只录入,不判断
拓扑排序,用一个队列,如果度等于0,入队,然后找以这个点为前缀的点,改变度,直到队列为空
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
int v[50],tep[50],du[50],mp[50][50],k[50];
int n;
void init()
{
for(int i=0;i<n;i++)
{
du[i]=0;
}
memset(mp,0,sizeof(mp));
}
int topsort()
{
memset(v,0,sizeof(v));
queue<int >q;
while(!q.empty())q.pop();
int i,j;
for(i=0;i<n;i++)
{
if(du[i]==0)
{
v[i]=1;
q.push(i);
}
}
int ff=0,pos=0;
while(!q.empty())
{
if(q.size()>1)ff=1;
int t=q.front();
q.pop();
k[pos++]=t;
for(i=0;i<n;i++)
{
if(mp[t][i])
du[i]--;
}
for(i=0;i<n;i++)
{
if(!v[i] && du[i]==0)
{
v[i]=1;
q.push(i);
}
}
}
if(pos<n) return 1;
if(ff) return 2;
return 3;
}
int main()
{
int m,t,i,j;
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
{
break;
}
init();
char s[10];
int flag=2,ff=0,stop;
for(j=1;j<=m;j++)
{
scanf("%s",s);
if(ff)continue;
int a=s[0]-'A';
int b=s[2]-'A';
if(!mp[a][b])
{
mp[a][b]=1;
du[b]++;
}
for(i=0;i<n;i++)
tep[i]=du[i];
flag=topsort();
for(i=0;i<n;i++)
du[i]=tep[i];
if(flag!=2)
{
ff=1;
stop=j;
}
}
if(flag==3)
{
printf("Sorted sequence determined after %d relations: ",stop);
for(i=0;i<n;i++)
{
printf("%c",k[i]+'A');
}
printf(".\n");
}
else if(flag==1)
{
printf("Inconsistency found after %d relations.\n",stop);
}
else
{
printf("Sorted sequence cannot be determined.\n");
}
}
}