您的位置:首页 > 其它

Sorting It All Out poj1094 (拓扑排序的变形 自己感觉对拓扑理解很有意义)

2016-12-04 11:29 471 查看
[align=center]Sorting It All Out[/align]

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33961 Accepted: 11884
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.


就是问你在第几个关系表达式(操作)之后能保证唯一确定的拓扑序列

!如果存在则输出

操作数以及唯一拓扑序列

!!如果不存在则输出

1.拓扑序列不唯一确定(很显然 如果存在不唯一入度为0的点就出现这种状况)

2.在第几步出现环

Accode :

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

int indegree[33]; ///入度
bool Map[33][33]; ///临接矩阵
char out[33]; ///拓扑排序输出数组

int Top(int n)
{
priority_queue <int,vector<int>, greater<int> > q;
/// 优先队列/队列/栈此题都可 为了统一自己拓扑就用优先队列吧
int i,k;
int zeroflag=0; ///1表示有多个入度0点
int num=0,temp[33]; ///临时入度数组
memcpy(temp,indegree,sizeof(indegree)); ///indegree复制过来

for(i=0; i<n; i++)
{
if(temp[i]==0)
{
q.push(i);
}
}
while(!q.empty())
{
if(q.size()>1)
{
zeroflag=1;
}
///要注意在while里面判断 因为此过程中也可能出现入度0不止一个的情况
k=q.top();
q.pop();
num++;
out[num]=k+'A'; ///num计数 以存到out里面
for(i=0; i<n; i++)
{
if(Map[k][i]==1 && --temp[i]==0)
{
q.push(i);
}
}
}

if(num!=n)
{
return 1; ///说明有环存在
}
else if(zeroflag==1)
{
return 0; ///存在多个入度为0的点 (要注意先判断环存在 在过程中入度0不唯一不等于整个程序入度0不唯一)
}
return -1; /// num==n 并且入度0唯一 说明拓扑排序完成
}

int main()
{
int n,m;
int i,k;
int step; ///记录操作数
int circleflag,orderflag; ///环  排序完成
char s[6];

while(cin>>n>>m && n||m)
{
circleflag=0;
orderflag=0;
memset(Map,0,sizeof(Map));
memset(indegree,0,sizeof(indegree));

for(i=1; i<=m; i++)
{
cin>>s;
if(circleflag == 0 && orderflag == 0) ///一旦有了结果 就不读图了
{
if(Map[s[2]-'A'][s[0]-'A'] == 1)
{
circleflag=1; ///表示有环了
cout<<"Inconsistency found after "<<i<<" relations."<<endl;
continue ;
} ///一定要在大if里面判断
if(Map[s[0]-'A'][s[2]-'A'] == 0)
{
Map[s[0]-'A'][s[2]-'A'] = 1;
indegree[s[2]-'A']++;
}
k=Top(n);
if(k==1)
{
circleflag=1; ///表示有环了
cout<<"Inconsistency found after "<<i<<" relations."<<endl;
}
else if(k==-1)
{
orderflag=1;
step=i; ///记录打印的位置 即操作步骤是第几
}
}
}
if(circleflag == 0 && orderflag == 0)
{
cout<<"Sorted sequence cannot be determined."<<endl; ///都没发生
}
else if(orderflag == 1)
{
cout<<"Sorted sequence determined after "<<step<<" relations: ";
for(i=1; i<=n; i++)
{
cout<<out[i];
}
cout<<"."<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息