Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30236 | Accepted: 10467 |
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.
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<queue>
using namespace std;
int n,s,cnt,flag;
int in[30],head[30],vis[30],rec[30],ans[30];
struct node
{
int a,b,next;
};
node edge[10010];
void add(int a,int b)
{
edge[cnt].a=a;
edge[cnt].b=b;
edge[cnt].next=head[a];
head[a]=cnt++;
}
void top_sort()
{
queue<int>q;
memset(ans,0,sizeof(ans));
for(int i=0;i<n;++i)
rec[i]=in[i];
for(int i=0;i<n;++i)
{
if(vis[i]&&rec[i]==0)
q.push(i);
}
int u,v,num=0,tem=0;
while(!q.empty())
{
if(q.size()>1)
tem=1;//每次只有一个入度为零的点才能唯一确定
u=q.front();
ans[num++]=u;
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].b;
rec[v]--;
if(rec[v]==0)
q.push(v);
}
}
if(!tem&&num==n)
flag=2;
if(num==s&&s<n)
flag=0;
if(num<s)
flag=1;
}
int main()
{
int m,a,b,mark;
char c[5];
while(scanf("%d%d",&n,&m)&&(n||m))
{
s=0;
cnt=0;
flag=0;
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
for(int i=1;i<=m;++i)
{
scanf("%s",c);
if(flag)
continue;
a=c[0]-'A';
b=c[2]-'A';
if(!vis[a])
{
vis[a]=1;
s++;
}
if(!vis[b])
{
vis[b]=1;
s++;
}
add(a,b);
in[b]++;
top_sort();
if(flag==2)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;++j)
printf("%c",ans[j]+'A');
printf(".\n");
}
if(flag==1)
printf("Inconsistency found after %d relations.\n",i);
}
if(flag==0)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
拓扑习惯用邻接表写,第一次用邻接矩阵,参照大神的思想
#include<cstdio>
#include<cstring>
int n,k,flag;
int in[30],ans[30],map[30][30];
void top_sort()
{
int mark,rec[30];
k=0;flag=2;
for(int i=0;i<n;++i)
rec[i]=in[i];
for(int i=0;i<n;++i)
{
int num=0;
for(int j=0;j<n;++j)
{
if(rec[j]==0)
{
mark=j;
num++;
}
}
if(num==0)//判断成环
{
flag=1;
return ;
}
if(num>1)
flag=0;//不能唯一确定
ans[k++]=mark;
rec[mark]=-1;
for(int j=0;j<n;++j)
{
if(map[mark][j])
rec[j]--;
}
}
}
int main()
{
int m,a,b;
char c[5];
while(scanf("%d%d",&n,&m)&&(n||m))
{
flag=0;
memset(in,0,sizeof(in));
memset(map,0,sizeof(map));
for(int i=1;i<=m;++i)
{
scanf("%s",c);
if(flag)
continue;
a=c[0]-'A';
b=c[2]-'A';
map[a][b]=1;
in[b]++;
top_sort();
if(flag==2)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<k;++j)
printf("%c",ans[j]+'A');
printf(".\n");
}
if(flag==1)
{
printf("Inconsistency found after %d relations.\n",i);
}
}
if(flag==0)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}