您的位置:首页 > 其它

POJ 1094 Sorting It All Out

2013-04-07 21:21 267 查看
题意:给你N个字母( 输入时保证是字母表的前n个 ), M个关系( 关系全部为 X<Y 的格式

输出的答案有三种:

1.根据所给的关系可以确定唯一的拓扑排序( 还要记录在给出的前几组就可以确定,不一定用到所有的关系 )

2.所给的关系有相互矛盾的(同样要记录出根据前几组就可以得出这个结论)

3.用上所有的关系仍然不能确定出拓扑排序

首先,这道题用拓扑排序毋庸置疑,但是在排序的时候要做一些修改,题目的难点也就是在这里

具体详见代码:

Sorting It All Out

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 23043Accepted: 7965
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.


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 27

int in[MAXN], temp[MAXN], topo[MAXN]; //入度,topo排序结果
bool visit[MAXN][MAXN]; //标记i, j存在i < j关系
int n, m;//字母数,边数

int toposort() //拓扑排序,三个返回值:环、继续读边(未完成)、排序完成
{
int u, v, count;
int num; //入度为0的字母
bool flag;
count = 1; //已排序字母个数
flag = true;
memset(topo, 0, sizeof(topo));
for(int i = 1; i <= n; ++i)
temp[i] = in[i];
for(int i = 1; i <= n; ++i)
{
num = 0; //这个不能写在外面,删边后度变化。。。。错了N久啊。。。
for(int j = 1; j <= n; ++j) //统计入度为0的字母个数
if(temp[j] == 0)
{
u = j;
num++;
}
if(num == 0) //为环
return 0;
if(num > 1) //多条分支,继续读边
flag = false;
temp[u] = -1;
topo[count++] = u; //加入拓扑序列
for(int j = 1; j <= n; ++j) //删边操作
if(visit[u][j] == true)
temp[j]--;
}
if(flag == false)
return -1;
return 1; //经过n次排序到达这里,则排序完成
}

int main()
{
char str[5];
int start, end;
bool flag;
int res; //toposort返回的结果
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0)
break;
memset(in, 0, sizeof(in));
memset(visit, false, sizeof(visit));
flag = true;
for(int i = 1; i <= m; ++i)
{
scanf("%s", str);
if(flag == false) //出现环或者矛盾
continue;
start = str[0] - 'A' + 1;
end = str[2] - 'A' + 1;
in[end]++;
visit[start][end] = true;
res = toposort(); //加一次边拓扑排序一次
if(res == 0)
{
printf("Inconsistency found after %d relations.\n", i);
flag = false;
}
else if(res == 1)
{
printf("Sorted sequence determined after %d relations: ", i);
for(int j = 1; j <= n; ++j)
printf("%c", topo[j] + 'A' - 1);
printf(".\n");
flag = false;
}
}
if(flag == true)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: