拓扑排序---(poj 1094)
来源:互联网 发布:淘宝中的查询热度 编辑:程序博客网 时间:2024/06/13 22:03
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36338 Accepted: 12786
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.
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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
提示:一个基于拓扑排序的算法,就是输出输出和输入不好弄;
#include <iostream>#include <cstring>#include <cstdio>#include <stack>using namespace std;const int N = 30;int in[N]; //入度bool Map[N][N]; //临接矩阵此题可以随便用char out[N]; //拓扑排序输出数组int topsort( int n ) {//传入参数为节点数; stack<int> q; int zeroflag = 0; int num = 0,temp[N]; memcpy(temp,in,sizeof(in));//拷贝,为了将原来in里面的数据保存,目的就是这是为了下一次的拓扑能继续; for( int i = 0; i < n; i++ ) if( !in[i] ) q.push(i); while( !q.empty() ) { if( q.size() > 1 )//这就说明是无序的;然而题目要求是有序的; zeroflag = 1;//要注意在while里面判断 因为此过程中也可能出现入度0不止一个的情况; int k = q.top(); q.pop(); num++; out[num] = k + 'A'; //num计数 以存到out里面 for(int i = 0; i < n; i++ ) if(Map[k][i]==true && --temp[i] == 0) q.push(i); } if( num != n ) return 1;//表示有环 if( zeroflag == 1 ) return 0;//多个点入度为零 return -1;//表示无环}int main(){ int n,m; char s[6]; while(~scanf("%d%d", &n, &m) &&n&&m){ int step = 0; //记录操作数 int circleflag=0,orderflag=0; memset(Map,0,sizeof(Map)); memset(in,0,sizeof(in)); for( int i = 1; i <= m; i++ ){ scanf("%s", s); if( circleflag == 0 && orderflag == 0 ){ //已经判出了 就不读了 if(Map[s[2]-'A'][s[0]-'A'] == true){//有关系就为true; circleflag=1; //有环了 printf("Inconsistency found after %d relations.\n", i); continue ; } if(Map[s[0]-'A'][s[2]-'A'] == false){//读入第一组数据; Map[s[0]-'A'][s[2]-'A'] = true; in[s[2]-'A']++;//入读; } int k = topsort(n);//拓扑排序; if( k == 1 ){//有环; circleflag=1; printf("Inconsistency found after %d relations.\n", i); } else if( k == -1 ){//如果已经构成一个有向无环图; orderflag=1; step=i; //记录位置 } } } if(circleflag == 0 && orderflag == 0) printf("Sorted sequence cannot be determined.\n"); else if(orderflag == 1){ printf("Sorted sequence determined after %d relations: ", step); for(int i=1; i<=n; i++) printf("%c", out[i]); printf(".\n"); } } return 0;}
阅读全文
0 0
- 拓扑排序,poj 1094
- 拓扑排序,poj 1094
- POJ 1094 拓扑排序
- poj-1094拓扑排序
- poj 1094 拓扑排序
- poj 1094 拓扑排序
- POJ 1094(拓扑排序)
- POJ 1094(拓扑排序)
- poj 1094 拓扑排序
- poj 1094 (拓扑排序)
- POJ 1094 拓扑排序
- POJ 1094 拓扑排序
- Poj 1094 拓扑排序
- POJ 1094 拓扑排序
- poj 1094 拓扑排序
- poj 1094 拓扑排序
- poj 1094 拓扑排序
- POJ 1094 拓扑排序
- Quartz框架从入门到实战
- input 选取文件夹及多个文件 识别内部文件
- ubantu怎么配置静态ip
- 开发工具集
- 重构查询的方式
- 拓扑排序---(poj 1094)
- 4用于cifar10的卷积神经网络-4.12优化器MomentumOptimizer的汇总结果分析
- FZU
- Android软键盘总结(笔记)
- Weblogic (一 /安装)
- Tablayout实现导航栏
- 怎样预防开车引起的腰痛?
- 用鼠标选择模型表面两点并连线
- android TabLayout 设置图标后,图标与文字之间间隔太大