POJ 1094 拓扑排序
Sorting It All Out
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24712 | Accepted: 8565 |
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.
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.
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.
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.
题意:根据题目所给的字母关系,确定拓扑排序情况。
解题思路:由于此题要输出在多少条关系下发生冲突,或者排序关系确定,或者所有的关系,或者仍然有不确定的关系,所以每加入一条边,拓扑排序一次,知道排序完成,或者发现冲突,或者加完m条边。
假如存在不确定关系,也存在冲突,那么冲突的优先级要高。
代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
int in[50],head[50],tol,num[50],cnt,m,n;
struct node
{
int next,to;
}edge[10000];
struct Node
{
int st,ed;
}pp[400];
void add(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
int topo()
{
int i,j,k;
int ind[100];
cnt=0;
int flag=1;
queue<int> q;
for(i=0;i<n;i++)
{
ind[i]=in[i];
if(ind[i]==0)q.push(i);
}
while(!q.empty())
{
if(q.size()>1)flag=0;
int u=q.front();q.pop();
num[cnt++]=u;
ind[u]--;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(--ind[v]==0)q.push(v);
}
}
//cout<<cnt<<endl;
if(cnt<n)return -1;
if(flag==0)return 0;
return 1;
}
void solve()
{
int i,j,k,p,q;
memset(in,0,sizeof(in));
memset(head,-1,sizeof(head));tol=0;
for(p=1;p<=m;p++)
{
add(pp[p].st,pp[p].ed);
in[pp[p].ed]++;
q=topo();
if(q==-1)
{
printf("Inconsistency found after %d relations.\n",p);
return;
}
if(q==1)
{
printf("Sorted sequence determined after %d relations: ",p);
for(i=0;i<cnt;i++)printf("%c",num[i]+'A');
puts(".");
return;
}
}
puts("Sorted sequence cannot be determined.");
}
int main()
{
int i,j,k;
char str[10];
//freopen("3.in","r",stdin);
// freopen("data.out","w",stdout);
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
for(i=1;i<=m;i++)
{
scanf("%s",str);
int t1=str[0]-'A';
int t2=str[2]-'A';
if(str[1]=='>')pp[i].st=t2,pp[i].ed=t1;
else pp[i].st=t1,pp[i].ed=t2;
}
solve();
// for(i=1;i<=m;i++)cout<<pp[i].st<<" "<<pp[i].ed<<endl;
}
return 0;
}

浙公网安备 33010602011771号