Sorting It All Out 【拓扑】
来源:互联网 发布:淘宝卖家提现 编辑:程序博客网 时间:2024/06/05 06:27
学习了,
第一道 拓扑
求拓扑的 算法思路
拓扑排序。拓扑排序:若G包含有向边(U,V),则在序列中U出现在V之前,即该序列使得图中所有有向边均从左指向右。如果图是有回路的,就不存在这样的序列。首先选择一个无前驱的顶点(即入度为0的顶点,图中至少应该有一个这样的顶点,否则肯定存在回路),然后从图中移去该顶点以及由其发出的所有有向边,如果图中还存在无前驱的顶点,则重复上述操作,直到操作无法进行。如果图不为空,说明图中存在回路,无法进行拓扑排序;否则移出的顶点的顺序就是对该图的一个拓扑排序。
此题 的还有一个 坑点就是,对于每种情况的输出是有优先级的,只要成环一定输出,所以当当前的 序列为无序的时候,也不能输出,因为之后可能导致成环 ,。。逻辑
代码
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
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 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<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define M 50#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;int n,m; int mp[M][M]; // 建图 int in[M]; // 入度情况 int mark[M]; // 每个条件之后,的入度情况 int topo[M]; //存储着topo序列 void init() // 初始化 { CLR(in,0); CLR(mp,0);}void trans() // 赋值给 标记数组 { int i,j; for(i=1;i<=n;i++) mark[i]=in[i];}int toposort(){ int num; // 记录 每一次 找的时候有几个 入度为0 的个数 int t=0; // 标记 topo 的数组 int i,j; int flag=1; // 记录 是无序还是 有序 int next; // 记录下一个将要并入 topo的序号 trans(); // 获取一次当前 的入度情况 for(i=1;i<=n;i++) { num=0; for(j=1;j<=n;j++) { if(!mark[j]) { num++; next=j; } } if(!num) return 0; // 成环了 if(num>1) flag=-1; // 这里可以判定 一定是无序的,但是 之后还可能是成环的情况 所以这里只能标记 topo[t++]=next; // 并入数组 mark[next]=-1; // 将next 这个顶点删除掉 for(j=1;j<=n;j++) { if(mp[next][j]) //以next为起点的有向边都删除掉 mark[j]--; } } return flag;}int main(){ int i,j; while(~scanf("%d%d",&n,&m)&&(n||m)) { init(); char c[5];int a,b; int exist;int flag=0; for( i=0;i<m;i++) { scanf("%s",c); if(flag) continue; //虽然已经 得出结果,但是 输入还没有完,要把输入输完 a=c[0]-'A'+1; b=c[2]-'A'+1; mp[a][b]=1; in[b]++; exist=toposort(); if(!exist) printf("Inconsistency found after %d relations.\n",i+1),flag=1; else if(exist==1) // { printf("Sorted sequence determined after %d relations: ",i+1); for( j=0;j<n;j++) printf("%c",topo[j]+'A'-1); printf(".\n"); flag=1; } } if(!flag) // 既没有 成环,也没有排序成功 printf("Sorted sequence cannot be determined.\n"); } return 0;}
- Sorting It All Out 拓扑
- Sorting It All Out 【拓扑】
- 【图论】【拓扑】 pku1094 Sorting It All Out
- poj1094 Sorting It All Out (拓扑排序)
- Poj1094 Sorting It All Out (拓扑排序)
- POJ1094-Sorting It All Out 拓扑排序
- 拓扑排序Sorting It All Out
- poj1094 Sorting It All Out(拓扑排序)
- POJ1094 Sorting It All Out 拓扑排序
- POJ1094 Sorting It All Out(拓扑排序)
- nyoj349 Sorting It All Out(拓扑排序)
- POJ1094 Sorting It All Out(拓扑排序)
- NYOJ_349 Sorting It All Out 拓扑排序
- poj1094 Sorting It All Out (拓扑排序)
- poj1094 Sorting it all out(拓扑排序)
- poj1094 Sorting It All Out 拓扑排序
- Sorting It All Out(拓扑排序)
- POJ1094 Sorting It All Out(拓扑排序)
- C++中的重载,覆盖,隐藏与多态
- sdut_java_救基友记2
- Android登录界面用SharedPreferences实现记住密码功能
- hdu 3549 Flow Problem(最大流模板题)
- Mysql服务无法启动,“服务没有报告任何错误”
- Sorting It All Out 【拓扑】
- IVRE中如何分析实时获取的数据包?
- python——字符串——编码
- 【群面】瓜子二手车-产品经理实习生(泪。。)
- 丘成桐谈数学
- 下一个幸运数(AOJ840)
- ML_2_Bayesian_Perspective
- java String类中的方法将一句英文单词的首字母转成大写
- 通俗理解卷积神经网络