Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11772 Accepted: 3845
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.
这是一道很让人orz的题目。也许是小虾我水平太弱。。。。
代码写了3000b+
三种情况,判断的有点软乱,不过最后还是出来了,哈哈
总结一些拓扑排序的经验
1.如果队列中有超过一个的节点,那一定是不稳定的
2.如果由出度考虑,可以实现由小到大排列
3.如果到结束还没有判断出来,那么就是无法判断
4.如果当中成功了,下面的就不用管了。
A_C CODE
bool tflag=true;//出现zero.size为一
int topo(int len)
{
if(zero.size()>1)
tflag=false;
if(zero.size()==0&&outSize==len&&tflag==true)
return 0;//成功找到
if(zero.size()==0&&outSize==visPos)
return 1;//和当前的个数一样,不能确定
if(zero.size()==0)
return 2;//出现环
int max=zero.top();
out[outSize++]=max;
zero.pop();
for( int i=0 ; i<point[max].len ; i++ )
{
int tmpPos=point[max].v[i];
rise++;
point[tmpPos].rudu--;
if( point[tmpPos].rudu==0 )
{
zero.push(tmpPos);
}
}
topo(len);
}
void copy(int n)
{
for(int i=1;i<=n;i++)
{
point[i].chudu=scrPoint[i].chudu;
point[i].rudu=scrPoint[i].rudu;
}
}
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
return 0;
int flag=0,outflag=0,iflag=true,iiflag=true;
bool need=true;
rise=0;
int ask;
outSize=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
point[i].chudu=scrPoint[i].chudu=0;
point[i].rudu=scrPoint[i].rudu=0;
point[i].len=scrPoint[i].len=0;
point[i].vis=false;
}
char a,b,c;
visPos=0;//当前没有节点
for(int i=1;i<=m;i++)
{
rise=0;
outSize=0;
tflag=true;
while(!zero.empty())
{
zero.pop();
}
//每加入一个新的数字,就判断一次
getchar();
scanf("%c%c%c",&a,&b,&c);
a-='A'-1;
c-='A'-1;
if(!vis[a])//如果以前没有访问过,则加入队列
{
vis[a]=1;
visPos++;
}
if(!vis[c])
{
vis[c]=1;
visPos++;
}
if(b=='<')
swap(a,c);
scrPoint[a].rudu++;
scrPoint[c].chudu++;
point[c].v[point[c].len++]=a;//只改变长度
copy(n);//使用scrPoint对前面的数据重新赋值
for(int j=1;j<=n;j++)//对zero队列的初始化
{
if(point[j].rudu==0&&vis[j])//控制输入,如果没有出现过的vis为false
{
zero.push(j);
rise++;
}
}
flag=topo(n);//topo排序并对结果处理
flagout[i]=flag;
if(flag==0&&need==true)
{outflag=flag;ask=i;need=false;}
if(need==true)//need为真也是判断条件,因为题目意思如果前面的条件判断为真,后面为假也是真
if(flag==2)
{
outflag=flag;ask=i;
}
}//大for循环结束,好大的一个for,下次总结应该写成函数
for(int i=m-1;i>=1;i--)//如果在某点形成闭环了,在之后的情况下也会闭环,所以要寻找第一个闭环
if(flagout[i]==2)
{
flag=2;
ask=i;
}
else break;
if(flag == 1)//循环到m还是无法判断
cout<<"Sorted sequence cannot be determined."<<endl;
else
{
if(outflag == 0)//outsize的长度正好为n,成功.
{
printf("Sorted sequence determined after %d relations: ",ask);//输出排列
{
for(int i=0;i<n-1;i++)
printf("%c",out[i]+'A'-1);
printf("%c",out[n-1]+'A'-1);
cout<<"."<<endl;
}
}
if(outflag==2)
printf("Inconsistency found after %d relations./n",ask);
}
}
}