求一个有向图的拓扑序列也是图论的基本题型。但是一般不会显式的看出题意是求拓扑序列或者求是否存在拓扑序列。拓扑序列一般用来判断一个图是否是一个有向无环图,如果一个图存在符合拓扑次序的序列则该图是有向无环图,反之则不是。
求拓扑序列步骤:
1,找到一个入度为0的点作为拓扑序列的第一个点
2,把该点和该点所有的边从图中删去
3,再在新的图中选择一个入度为0的点作为拓扑系列的第二个点
...以此类推,如果在所有节点尚未删去时找不到入度为0的点则说明剩余节点存在环路,不存在拓扑序列
-
九度1448:Legal or Not 题目地址:http://ac.jobdu.com/problem.php?pid=1448
题目描述:
-
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
-
输入: