Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32630 | Accepted: 11343 |
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.
英文不是很好,刚开始读题没有判断清楚啊!!!继续加油
题意:
就是在输入的适合判断是不是成环,是不是有序,是不是无序
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.
这句话就是表示需要进行在线处理的
成环:在topo排序中肯定会出现入度没有 0 的情况,这个时候就可以判断有环
无序:就是有多个入度 为 0 的点
注意:
在线处理需要注意就是每一次输入更新indegree数组后,在topo排序中需要复制这个数组
防止每一次的topo 排序把indegree数组改变,这个数组不能改变的,因为是逐渐累积成需要的indegree数组的
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
int m,n;
int map[30][30];
int indegree[30];
int temp_indegree[30];
char ans[30];
int topo_sort()
{
for(int i=0;i<m;i++)
indegree[i]=temp_indegree[i];
int flag=1;//有序
for(int i=0;i<m;i++){
int pos=-1;
int cnt=0;
for(int j=0;j<m;j++)
if(!indegree[j]){
pos=j;
cnt++;
}
//-------------------------------------------------------//
if(cnt==0)//有环
return 0;
else if(cnt>1)
flag=-1;//无序
//-------------------------------------------------------//
indegree[pos]=-1;
ans[i]=pos+'A';
for(int j=0;j<m;j++)
if(map[pos][j])
indegree[j]--;
}
ans[m]='\0';
return flag;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&m,&n),m+n)
{
char str[5];
mem(map);
mem(temp_indegree);
mem(ans);
//-------------------------------------------------------//
int sign=1;
for(int i=0;i<n;i++){
scanf("%s",str);
if(!sign)
continue;
//-------------------------------------------------------//
int t1=str[0]-'A';
int t2=str[2]-'A';
map[t1][t2]=1;
temp_indegree[t2]++;
int temp=topo_sort();
//-------------------------------------------------------//
if(temp==0){
printf("Inconsistency found after %d relations.\n",i+1);
sign=0;
}
else if(temp==1){
printf("Sorted sequence determined after %d relations: %s.\n",i+1,ans);
sign=0;
}
}
if(sign)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}