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.
题意:
给你字母表的前N个大写字母和M个大小关系(即有向边)。所有的边按顺序给你,要你在能判断出全序或矛盾的时候及时输出(即当输入i条边的时候,出现了有向环或已经全序了,那么应该及时输出,),或者直到最后也不能出现全序。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=26+5;
vector<int> G[maxn];
int in[maxn];
int n,m;
char order[1000][10];
char ans[maxn];
int cnt;//记录ans中当前字母
int topo()
{
cnt=0;
queue<int> Q;
int indegree[maxn]; //错误1,这里要重新用一个入度数组
memcpy(indegree,in,sizeof(in));
bool all_order=true;//全序标记
for(int i=0;i<n;i++)if(indegree[i]==0) Q.push(i); //错误2,忘了入度0才入队列
while(!Q.empty())
{
if(Q.size()>1) all_order=false;//Q中始终只有1个元素才能保证全序
int u=Q.front(); Q.pop();
ans[cnt++]=u+'A';
for(int i=0; i<G[u].size(); i++)
{
int v=G[u][i];
if(--indegree[v]==0) Q.push(v);
}
}
int result = 0;
if(cnt<n) result = -1;//存在环
else if(all_order==true) result=1;//已然全序
return result;
}
int main()
{
while(scanf("%d%d",&n,&m)==2&&n)
{
memset(in,0,sizeof(in));
for(int i=0;i<n;i++) G[i].clear();
for(int i=0;i<m;i++)
scanf("%s",order[i]);
int flag=0,i;
for(i=0;i<m;i++)
{
int u=order[i][0]-'A', v=order[i][2]-'A';
G[u].push_back(v);
in[v]++;
if( (flag=topo())!=0) break;
}
ans[n]=0; //错误,这句没加,这个错误很难找
if(flag==1) printf("Sorted sequence determined after %d relations: %s.\n",i+1,ans);
else if(flag==0) printf("Sorted sequence cannot be determined.\n");
else if(flag==-1) printf("Inconsistency found after %d relations.\n",i+1);
}
return 0;
}