Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 35774 Accepted: 12595
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 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.
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.
4 6
A
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
Source
East Central North America 2001
题目给出N个字母(从A开始的依次N个字母),M对字母之间的关系。让你判断是否能排好序,或是出现矛盾,或是都不能完成。关键在于理解题意,如果前一部分已经将N个字母排好序了,则后面的即使发生冲突也不用管,需要输出完成排序的关系序号。如果发生冲突,也需要输出冲突的关系序号。
理解好题意之后,题解就比较简单了,就是每输入一对关系,都进行一次拓扑排序,判断一下是否已经完成排序或是发出冲突(即存在环)即可,如果有以上情况,则之后的都无需再判断,如果最后都没有完成排序,输出没有完成即可。
#include
#include
#include
#include
#include
#include
using namespace std;
int N, M;
int mat[30][30], in[30], que[30], tot, vis[30], tmp[30];
void init()
{
memset(mat, 0, sizeof mat);
memset(in, 0, sizeof in);
tot = 0;
}
int topsort()
{
tot = 0;
int cnt = 0, ret = 0;
int src;
memcpy(tmp, in, sizeof tmp);
for(int i = 1; i <= N; i++)
{
cnt = 0;
for(int j = 1; j <= N; j++)
{
if(tmp[j] == 0){
cnt++;
src = j;
}
}
if(cnt == 0) return -1;
if(cnt > 1) ret = 1;
que[tot++] = src;
tmp[src] = -1;
for(int j = 1; j <= N; j++)
if(mat[src][j]) tmp[j]--;
}
return ret;
}
int main()
{
while(~scanf("%d%d", &N, &M))
{
init();
int flag = 1, err, rit;
if(N == 0 && M == 0) break;
for(int i = 1; i <= M; i++)
{
char str[10];
int u, v;
scanf("%s", str);
u = str[0] - 'A' + 1, v = str[2] - 'A' + 1;
mat[u][v] = 1;
in[v]++;
if(flag == 1)
{
flag = topsort();
if(flag == -1){
err = i;
printf("Inconsistency found after %d relations.\n", err);
}
if(flag == 0){
rit = i;
printf("Sorted sequence determined after %d relations: ", rit);
for(int i = 0; i < N; i++)
printf("%c", que[i] - 1 + 'A');
printf(".\n");
}
}
}
if(flag == 1)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}