poj 1094 Sorting It All Out (拓扑排序)

效率太低了,反思~~~~

1:题意理解错误

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. 

明明没说是按它列的顺序求解--关系的数量,

2:对环的处理上没有搞好 

#include<stdio.h>
#include<string.h>
#define N 30
int map[N][N],sum[N],temp[N];
char alp[N];
int n,m;
int topology()
{
int t=1,zero,k;
memset(alp,'\0',sizeof(alp));
memcpy(temp,sum,sizeof(temp));
for(int i=0;i<n;i++)
{
zero=0;
for(int j=0;j<n;j++)
if(!temp[j])
{
zero++;k=j;
}
if(zero>1) t=-1;
if(zero==0) return 0;
temp[k]=-1,alp[i]='A'+k;
for(int j=0;j<n;j++)
{
if(map[k][j])
{
temp[j]--;
}
}

}
return t;
}
int main()
{
while(scanf("%d%d%*c",&n,&m),n+m)
{
memset(map,0,sizeof(map));
memset(sum,0,sizeof(sum));
int ok=0;
for(int i=1;i<=m;i++)
{
char st[4];gets(st);
if(!map[st[0]-'A'][st[2]-'A'])
{
map[st[0]-'A'][st[2]-'A']=1;
sum[st[2]-'A']++;
}

if(ok) continue;
int t=topology();
if(t==1)
{
ok=1;
printf("Sorted sequence determined after %d relations: %s.\n",i,alp);
}
else if(!t)
{
ok=1;
printf("Inconsistency found after %d relations.\n",i);
}
}
if(!ok)
printf("Sorted sequence cannot be determined.\n");

}
//system("pause");
}



你可能感兴趣的:(sort)