CF1062F.Upgrading Cities

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn cities in the kingdom XX , numbered from 11 through nn . People travel between cities by some one-way roads. As a passenger, JATC finds it weird that from any city uu , he can't start a trip in it and then return back to it using the roads of the kingdom. That is, the kingdom can be viewed as an acyclic graph.

Being annoyed by the traveling system, JATC decides to meet the king and ask him to do something. In response, the king says that he will upgrade some cities to make it easier to travel. Because of the budget, the king will only upgrade those cities that are important or semi-important. A city uu is called important if for every city vuv \neq u , there is either a path from uu to vv or a path from vv to uu . A city uu is called semi-important if it is not important and we can destroy exactly one city vuv \neq u so that uu becomes important.

The king will start to act as soon as he finds out all those cities. Please help him to speed up the process.

输入格式

The first line of the input contains two integers nn and mm ( 2n3000002 \le n \le 300\,000 , 1m3000001 \le m \le 300\,000 ) — the number of cities and the number of one-way roads.

Next mm lines describe the road system of the kingdom. Each of them contains two integers uiu_i and viv_i ( 1ui,vin1 \le u_i, v_i \le n , uiviu_i \neq v_i ), denoting one-way road from uiu_i to viv_i .

It is guaranteed, that the kingdoms' roads make an acyclic graph, which doesn't contain multiple edges and self-loops.

输出格式

Print a single integer — the number of cities that the king has to upgrade.

输入输出样例

  • 输入#1

    7 7
    1 2
    2 3
    3 4
    4 7
    2 5
    5 4
    6 4
    

    输出#1

    4
  • 输入#2

    6 7
    1 2
    2 3
    3 4
    1 5
    5 3
    2 6
    6 4
    

    输出#2

    4

说明/提示

In the first example:

- Starting at the city 11 we can reach all the other cities, except for the city 66 . Also, from the city 66 we cannot reach the city 11 . Therefore, if we destroy the city 66 then the city 11 will become important. So 11 is a semi-important city.

  • For city 22 , the set of cities that cannot reach 22 and cannot be reached by 22 is {6}\{6\} . Therefore, destroying city 66 will make the city 22 important. So city 22 is also semi-important.
  • For city 33 , the set is {5,6}\{5, 6\} . As you can see, destroying either city 55 or 66 will not make the city 33 important. Therefore, it is neither important nor semi-important.
  • For city 44 , the set is empty. So 44 is an important city.
  • The set for city 55 is {3,6}\{3, 6\} and the set for city 66 is {3,5}\{3, 5\} . Similarly to city 33 , both of them are not important nor semi-important.
  • The city 77 is important since we can reach it from all other cities.

So we have two important cities ( 44 and 77 ) and two semi-important cities ( 11 and 22 ).In the second example, the important cities are 11 and 44 . The semi-important cities are 22 and 33 .

首页