您的位置:首页 > 其它

Sorting It All Out 拓扑排序&&判断是否存在环,是否关系包含所有的点

2013-10-26 17:30 381 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.

Sorted sequence cannot be determined.
**************************************************************************************************************************经典的拓扑排序
***************************************************************************************************************************

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int a[26][26];
queue<char>Q1;//记录两者之间的关系
int de[26],de2[26];
int relations;
bool flag;
int kind,it,jt,kt,n,m;
void carry(int n,int ret)
{
int i,j,sum=0;
int num;
bool sign=true;
queue<int>Q;
for(i=0;i<n;i++)
{
if(de[i]==0)
Q.push(i);
}
while(!Q.empty())
{
num=0;
for(i=0;i<n;i++)
{
if(de[i]==0)
num++;
}
if(num>1)
sign=false;//存在不确定关系
i=Q.front();
Q.pop();
Q1.push(i+'A');//
sum++;
de[i]=-1;
for(j=0;j<n;j++)
{
if(a[i][j]==true)
{
de[j]--;
if(de[j]==0)
Q.push(j);
}
}

}
//如果存在确定关系,记录位置,不再往下算
if(sum==n&&sign)
{
flag=true;
kind=1;
relations=ret;
}
else
for(i=0;i<n;i++)
if(de[i]>0)//如果存在环,不再往下算
{
flag=true;
kind=2;
relations=ret;
break;
}
if(!flag)//如果都不满足,恢复
{
for(i=0;i<n;i++)
de[i]=de2[i];
}
return;

}
int main()
{
char left,mid,right;
int ni,nj;
while(scanf("%d%d",&n,&m))
{
getchar();
flag=false;
kind=0;
relations=0;
if(n==0&&m==0)
break;
for(it=0;it<n;it++)//初始化
{
for(jt=0;jt<n;jt++)
a[it][jt]=false;
de[it]=-1;
de2[it]=-1;
}
for(it=0;it<m;it++)
{
cin>>left>>mid>>right;
getchar();
ni=left-'A';//两点有联系时,入度先都置0
nj=right-'A';
if(de[ni]==-1)
{
de[ni]=0;
de2[ni]=0;
}
if(de[nj]==-1)
{
de[nj]=0;
de2[nj]=0;
}
if(!a[ni][nj])
{
a[ni][nj]=true;//ni和nj有关系
de[nj]++;
de2[nj]++;
}
if(!flag)
{
while(!Q1.empty())//没找到确定关系时,要一直恢复
Q1.pop();
carry(n,it+1);
}
}
if(flag)
{
if(kind==1)
{
printf("Sorted sequence determined after %d relations: ",relations);
while(!Q1.empty())
{
cout<<Q1.front();
Q1.pop();
}
cout<<"."<<endl;
}
else
if(kind==2)
{
printf("Inconsistency found after %d relations.\n",relations);
}
}
else
printf("Sorted sequence cannot be determined.\n");

}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐