轻量级前端框架助力开发者提升项目效率与性能
564
2022-10-03
POJ 2186 Popular Cows (缩点)
Description
Every cow’s dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
Line 1: Two space-separated integers, N and M Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Line 1: A single integer that is the number of cows who are considered popular by every other cow.
Sample Input
3 31 22 12 3
Sample Output
1
题意
查找图中有多少个点,满足其他所有点都可以直接或者间接到达该点。
思路
首先,我们可以想到,最终的结果可能出现在原图的一个强连通子图中。
因此,对原图进行缩点,缩点以后的图是一个有向无环图,而此时,要想某个节点可以被其他任意点直接或者间接到达,则该点一定可以沿着反向边到达图中任意节点。
于是我们考虑反向建立缩点图,然后寻找一个入度为 0 的点开始 dfs ,若无法到达缩点图中其他所有点,则结果为 0 ,否则结果为起始点中所包含的所有原图点的个数。
AC 代码
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~