Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31877 | Accepted: 11086 |
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.
该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。是典型的拓扑排序,拓扑排序可以用栈来实现,每次入栈的是入度为0的节点。但输出格式上却有三种形式:
1.该字母序列有序,并依次输出;
2.该序列不能判断是否有序;
3.该序列字母次序之间有矛盾,即有环存在。
而这三种形式的判断是有顺序的:先判断是否有环(3),再判断是否有序(1),最后才能判断是否能得出结果(2)。注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,对后面的输入就不用做处理了。
#include <stdio.h>
#include <string.h>
int N, M;
int map[30][30];
int indegree[30];
int in[30];
int order[30];
int TopoSort()
{
memcpy(in, indegree, sizeof(indegree));
int cas = 1, len = 0, pos, num, i, j;
for(i = 1; i <= N; i++)
{
num = 0;
for(j = 1; j <= N; j++)
{
if(in[j] == 0)
{
num++;
pos = j;
}
}
if(num == 0) return 0;
if(num > 1) cas = -1;
order[len++] = pos;
in[pos] = -1;
for(j = 1; j <= N; j++)
if(map[pos][j] == 1)
in[j]--;
}
return cas;
}
int main()
{
int i, j, cas, x, y;
bool done;
char s[5];
while(scanf("%d%d", &N, &M) != EOF && N && M)
{
done = false;
memset(map, 0, sizeof(map));
memset(indegree, 0, sizeof(indegree));
for(i = 1; i <= M; i++)
{
scanf("%s", s);
if(done) continue;
x = s[0] - 'A' + 1;
y = s[2] - 'A' + 1;
if(map[y][x] == 1)
{
done = true;
printf("Inconsistency found after %d relations.\n", i);
continue;
}
map[x][y] = 1;
indegree[y]++;
cas = TopoSort();
if(cas == 1)
{
printf("Sorted sequence determined after %d relations: ", i);
for(j = 0; j < N; j++)
printf("%c", order[j] + 'A' - 1);
printf(".\n");
done = true;
}
else if(cas == 0)
{
printf("Inconsistency found after %d relations.\n", i);
done = true;
}
}
if(!done)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}