Language:
Sorting It All Out
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. Source |
这是做的拓扑排序的第一题。
题意:输入n和m,n 表示给出的字符的个数,然后输入m行 字符之间的关系,每输入一行便要判断是不是有环,是不是有n个字符唯一的排列顺序。若全部输入完毕后没有唯一的排列顺序,也没有环便输出 Sorted sequence cannot be determined.
分析:每输入一行 字符间的关系便调用拓扑排序的函数判断,在拓扑排序中,先定义一个flag=1,如果有环,直接返回0;如果不唯一,则改变flag的值;有唯一情况时,保存入度为0的字符。循环完毕后,返回flag的值。
在主函数中如果调用的函数返回1,那么表示有唯一排序,输出字符串;如果返回0,表示有环,停止循环。如果已输入m行,判断结果返回-1,那么输出 Sorted sequence cannot be determined.
#include <stdio.h>
#include <string.h>
int map[30][30];//记录图
int ans[30];//记录结果
int into[30];//记录入度
int into1[30];//同上
int n,m;
int topu()
{
int i;
int k;
int flag=1;
for(int j=1;j<=n;j++)//将入度保存到另一个数组中 ,确保into数组不变;
{
into1[j]=into[j];
}
for(i=1;i<=n;i++)
{
int count=0;
for(int j=1;j<=n;j++)//找是否存在入度为0的点
{
if(into1[j]==0)
{
count++;
k=j;
}
}
if(count>1)flag= -1;//入度为0的点不唯一,所以顺序也不唯一
if(count==0)return 0;//不存在入度为0的点,有环;
ans[i]=k;
into1[k]=-1;
for(int j=1;j<=n;j++)//将从这个字符出发的到达其他的节点的入度减1;
{
if(map[k][j]==1)
into1[j]--;
}
}
return flag;
}
int main()
{
while(scanf("%d%d",&n,&m),m,n)
{
memset(map,0,sizeof(map));//清空数组
memset(ans,0,sizeof(ans));
memset(into,0,sizeof(into));
char a[5];
int i;
int flag=0;
for(i=1;i<=m;i++)
{
scanf("%s",&a);
if(flag)continue; //已经输出过了,所以continue
int x=a[0]-'A'+1;
int y=a[2]-'A'+1;
if(!map[x][y])//消除重边
{
map[x][y]=1;
into[y]++;
}
int ans1=topu();//调用拓扑排序
if(ans1==0)
{
printf("Inconsistency found after %d relations.\n",i);
flag=1;
}
if(ans1==1)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=1;j<=n;j++)
printf("%c",ans[j]+'A'-1);
printf(".\n");
flag=1;
}
}
if(!flag)//表示没有输出过
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}