您的位置:首页 > 其它

[POJ](1094)Sorting It All Out ---拓扑排序(图)

2017-08-16 14:57 731 查看
Sorting It All Out

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 35706 Accepted: 12565
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

East Central North America 2001
解题新知:(*@ο@*) 哇~这道题看了好长时间,理解题意是关键,还要注意题意情况的优先级关系。/(ㄒoㄒ)/~~不说了,还是看前辈们的思路和做法吧。自己也是稍微理解,稍微改了改前辈的代码。

参考:http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html



AC代码:



#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int mmap[30][30];
int indegree[30];
//int copyindegree[30];
int llist[30];
int n,m;
int topoSort()
{
queue<int>q;
int copyindegree[30];
for(int i=0;i<n;i++)
{
copyindegree[i]=indegree[i];
if(!indegree[i])
q.push(i);
}
int flag=0;
int num=0;
int k=-1;
while(!q.empty())
{
if(q.size()>1)
flag=1;
k=q.front();
q.pop();
llist[num++]=k;
for(int i=0;i<n;i++)
{
if(mmap[k][i]==1)
{
copyindegree[i]--;
if(copyindegree[i]==0)
q.push(i);
}
}
}
if(num!=n)
return 1;
else if(flag)
return 2;
return 0;
}
int main()
{
while(cin>>n>>m && n && m)
{
int determined=0;//唯一升序序列确定
int inconsistency=0;//矛盾
char s[5];
int u,v;
int res;
memset(mmap,0,sizeof(mmap));
memset(indegree,0,sizeof(indegree));
memset(llist,0,sizeof(llist));
for(int i=1;i<=m;i++)
{
cin>>s;
u=s[0]-'A';
v=s[2]-'A';
if(!determined && !inconsistency)
{
if(mmap[v][u]==1)
{
inconsistency=1;
printf("Inconsistency found after %d relations.\n",i);
continue;//???
}
if(mmap[u][v]==0)
{
mmap[u][v]=1;
indegree[v]++;
}
res=topoSort();
if(res==0)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",llist[j]+'A');
printf(".\n");
determined=1;
}
else if(res==1)
{
printf("Inconsistency found after %d relations.\n",i);
inconsistency=1;
}
}
}
if(!determined && !inconsistency)
{
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: