Sorting It All Out
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 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.
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.
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.
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
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
思路:这道题目考察了拓扑排序的基本思想:每一步寻找一个入度为0的结点,然后删除之。将这个结点指向的结点入度减1。删除从这个结点出发的所有边
同时考察了对于一个有向图是否有环、是否严格有序的判断。(当发现多个结点的度为0时,则不是严格有序。当发现没有结点入度为0时,则有环)
判断函数中,所以如果发现有环直接返回,如果不是严格有序即多个入度为0的点,继续往下判断,知道最后才能确定状态
主函数中,如果能确定顺序了,或有环就可以直接输出答案了,但是不能确定要等所有输入都结束才能判断
coed:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
int G[30][30];
queue<int>ans_q;
int judge(){
int G1[30][30];//因为每次操作都要对地图进行更改但是原来的图不能改
memcpy(G1,G,sizeof(G));//所以建立一个地图副本进行修改判断
int i,j;
int in[30] = {0};//记录每个点的入度,并初始化为0
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(G[i][j]){
in[j]++;
}
}
}
int k = 0;//记录总的循环次数 总共n次
queue<int>q;//副本队列,记录当前情况下得到的序列
int flag = 0;//代表状态
while(k < n){
int cnt = 0;
for(i = 0; i < n; i++){
if(!in[i])
cnt++;
}//统计入度为0的点的个数
if(cnt == 0)
return 2;//没有入度为零的点说明有环,后面不用看了,直接返回2代表有环
if(cnt > 1)
flag = 3;//如果有多个,说明顺序还不能确定,继续往后看
//如果cnt==1,则flag继承的上次循环的flag,到最后一次循环时
//会发现如果不是有环的情况,入度为0的点都只有一个,因为就剩一个点了
//所以这里flag不做操作,就会继承上次循环的flag状态,这是很巧妙的地方
for(i = 0; i < n; i++){
if(!in[i]){
k++;
q.push(i);//把这个0度点入队
//下面就是把和这个点相连的边去掉,修改入度
for(j = 0; j < n; j++){
if(G1[i][j]){
in[j]--;
G1[i][j] = 0;
}
}
in[i] = -1;//最后把这个点的入度变为-1
break;//每次只删除一个点,对一个点进行拓扑排序
}
}
}
if(flag != 3){//如果flag不是3,首先肯定不是2,因为flag是2的话早就返回了
ans_q = q;//说明只能是1,说明每次恰好有唯一一个点入度为0,所以
return 1;//最终就确定了顺序
}
else//如果是3就返回3,说明最终在已知这些条件下不能判断
return 3;
}
int main(){
while(cin >> n >> m){
if(!n && !m)
break;
int i,j;
char left,right;
memset(G,0,sizeof(G));
int flag = 0;
for(i = 1; i <= m; i++){
cin >> left >> right >> right;
G[left-'A'][right-'A'] = 1;
if(flag == 1 || flag == 2)//flag为1或者2说明上一次循环已经输出的结果,以后的输入直接跳过
continue;
flag = judge();//核心部分,对每一次输入进行判断,看是否有环,是否已经能确定顺序或不能确定
if(flag == 1){//只要能够确定顺序,后面的输出都是多余的不影响顺序,所以直接输出结果,然后后面输入直接continue
cout << "Sorted sequence determined after " << i << " relations: ";
while(!ans_q.empty()){
int a = ans_q.front();
ans_q.pop();
cout << (char)('A'+a);
}
cout << "." << endl;
}
else if(flag == 2){//同理,只要是有环,整个序列后面不用看了,后面输入直接continue
cout << "Inconsistency found after " << i << " relations." << endl;
}
}
//整个输入结束最后才能判断这个序列能否确定顺序
if(flag == 3)
cout << "Sorted sequence cannot be determined." << endl;
}
return 0;
}