Sorting It All Out (拓扑排序)
发布日期:2021-10-16 05:05:07
浏览次数:15
分类:技术文章
本文共 2999 字,大约阅读时间需要 9 分钟。
Sorting It All Out
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 64 Accepted Submission(s) : 21
Problem 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:<br><br>Sorted sequence determined after xxx relations: yyy...y.<br>Sorted sequence cannot be determined.<br>Inconsistency found after xxx relations.<br><br>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.<br>
Sample Input
4 6A
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
Source
PKU
分析转自:
这题典型的拓扑排序,但是有点变化。
题目样例的三种输出分别是:
1. 在第x个关系中可以唯一的确定排序,并输出。
2. 在第x个关系中发现了有回环(Inconsisitency矛盾)
3.全部关系都没有发现上面两种情况,输出第3种.
那么对于给定的m个关系,一个个的读进去,每读进去一次就要进行一次拓扑排序,如果发现情况1和情况2,那么就不用再考虑后面的那些关系了,但是还要继续读完后面的关系(但不处理)。如果读完了所有关系,还没有出现情况1和情况2,那么就输出情况3.
拓扑排序有两种方法,一种是导论上的,一种是用贪心的思想,这题用贪心的思想做更好。
贪心的做法:
1. 找到所有入度为0的点, 加入队列Q
2.取出队列Q的一个点,把以这个点为起点,所有它的终点的入度都减1. 如果这个过程中发现经过减1后入度变为0的,把这个点加入队列Q。
3.重复步骤2,直到Q为空。
这个过程中,如果同时有多个点的入度为0,说明不能唯一确定关系。
如果结束之后,所得到的经过排序的点少于点的总数,那么说明有回环
代码;
#include#include #include #include #include #include #define inf 0x3f3f3f3fusing namespace std;int n,m;int mp[30][30],dis[30],ans[30],temp[30];int topo(){ stack q; //用栈存储入度为0的点 memcpy(temp,dis,sizeof(dis)); for(int i=0;i 1){flag0=1;} //如果同时有多个入度为0的点,则排列不是唯一确定的 int tem=q.top(); //取栈顶元素,即顶点 ans[sum++]=tem; //记录每个顶点的即大写字母出现顺序 q.pop(); //清除栈顶元素 for(int i=0;i >n>>m,n||m) { bool flag1=0,flag2=0; memset(mp,0,sizeof(mp));//记录关系 memset(dis,0,sizeof(dis));//存储入度 for(i=1;i<=m;i++) { cin>>str; if(!flag1&&!flag2) { if(mp[str[2]-'A'][str[0]-'A']==1){ //当存在 A
转载地址:https://blog.csdn.net/sinat_37668729/article/details/77016424 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月13日 02时18分54秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
打印php错误日志,php怎样打印错误日志
2019-04-21
mysql中用户线程作用,mysql用户线程的建立与用户线程的状态源码解析
2019-04-21
php页面引用公共文件,WeiPHP插件模板中快速引入公共模板文件
2019-04-21
php tracy,admin.php
2019-04-21
php访问父类的所有属性,php – 在父类中使用$this仅在子类中显示父类属性
2019-04-21
oracle比较强大的函数,SQL和ORACLE函数比较
2019-04-21
php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理
2019-04-21
php红包平均分配,红包平均分配算法
2019-04-21
linux磁盘的命令是,linux磁盘相关的命令
2019-04-21
linux 停用用户,linux – 如何禁用用户的网络访问?
2019-04-21
linux服务器 缓存,Linux服务器内存使用分析及内存缓存
2019-04-21
linux英文包安装教程视频,Linux源码包安装过程讲解
2019-04-21
linux 关闭rsync服务器,linux下配置rsync服务器和实时同步
2019-04-21
linux初始化TCP服务失败,深入Linux系统追踪TCP初始化
2019-04-21