拓撲排序 (判斷是否完全拓撲,及序列)
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;
}