poj2186

maksyuki 发表于 oj 分类,标签:
0

Popular Cows

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.

Source

USACO 2003 Fall

 

题目类型:有向图重连通分量(Tarjan)+缩点

算法分析:由于仰慕关系可以传递,则若一个强连通分量Q中的某个点被外部点A所仰慕,则Q中所有点都会被A仰慕。同样,若Q中的某个点仰慕某个外部点B,则Q中所有点都会仰慕B。所以可以缩点,找新构成的图中出度为0的点(注意这个点是可能是缩点!!!),若出度为0的点的个数不为1,则输出0。否则输出当前缩点中点的个数