POJ2186解题报告 强连通分支+缩点

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11739 Accepted: 4641

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 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.
     这道题刚开始我用的普通DFS直接搜索,不幸的是超内存了,因为我无知的开了一个长度为1亿的字符数组(PS:以后开大数组前一定要先算一下需要的内存),被MLE后不甘心继续修改,把内存降下来后继续提交,TLE,再改,再TLE。。若干次后我意识到普通算法好像过不去。。也没有心思再改了。。。
     看了讨论才知道这个需要用求强连通分量的tarjan算法,百度上找到tarjan,看了挺长时间才把他真正看懂。。
     看懂了tarjan,我又想了一个多小时,写出代码,终于将这道题AC。。真不容易,不过这道题让我学到了新的东西,A掉这题的成就感很强,在此我要感谢这道题~,还要感谢帮助我的小杰同学~!
思路:用tarjan算法求出图的强连通分量数C,将每个强联通分量看成一个点(缩点),然后求出每个缩点的出度,判断这些点的出度是否等于C-1,如果相等,则出度为0的那个连通分支所包含的点数即是所求,否则输出0。
证明:假设已经求出关系图G的各个连通分量,由连通分支内任意两点都可达的性质可以得到:该分支内的任意一点都能到达这个点。将每个连通分量缩成一个点,对于一个点来说,只要其他点存在到这个点的单向路径则这个缩点内包含的点都被其他的连通分支所包含的点支持,因此,题目也就是求关系图每个连通分支的出度,如果只存在一个出度为0的连通分支则这个联通分支内包含的点数即是所求,否则不存在被所有牛支持的牛。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值