c语言sscanf函数的用法是什么
221
2022-09-06
nyoj349 Sorting It All Out(拓扑排序)
题目349题目信息运行结果本题排行讨论区
Sorting It All Out
3000 ms | 内存限制: 65535
3
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.
样例输入
4 6 A
样例输出
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
来源 POJ
上传者 陈玉
刚开始没有理解题意 。。想了好两三天 哈哈(好吧 是我懒了)
拓扑排序过程:
1先建图,然后寻找入读为0的顶点
2:然后找到与这个顶点有关的边和点,删去边,点的入度-1
3.继续寻找下一个入度为0的顶点。。重复2.
总共三种情况 冲突 可以排序 信息不完整
判断冲突 起初我想错了 总想着并查集判断成环冲突 可以却发现我错的大大的 因为并查集判断的是集合 也就是无向的
而这道题是有向的。
比如A
其中判断冲突 实在输入的时候就判断的 可以排序和信息不完整在输入数据完毕后再检查。
拓扑排序处理信息不完整和冲突
1.如果出现两个同时入度为0的,并且根是自己就是信息不完整。
2.如果栈的大小刚好等于比较的数的个数 那么就是能够排序 如果栈的大小小于比较的数的个数 就是信息不完整
用的vector容器模拟邻接表。queue实现拓扑排序,stack存贮大小
#include
发表评论
暂时没有评论,来抢沙发吧~