#P11073. Game King

Game King

Problem Description

You have traveled to the world of Yu-Gi-Oh!, and now you are dueling against your opponent—a directed graph with nn vertices and mm edges.

Then I forgot what happened.

A true duelist believes everything is inevitable. To deal with the duel in front of you, you need to know how many vertices xx satisfy the following condition.

  • For any vertex yy, it holds that either xx can reach yy or yy can reach xx (we consider that a vertex can reach itself).

Input Format

The first line contains two positive integers n,mn, m, representing the number of vertices and the number of edges in the given directed graph.

The next mm lines each contain two positive integers x,yx, y, denoting a directed edge xyx \to y.

Output Format

Output one integer in one line: the answer, i.e., how many vertices satisfy that every vertex can reach it or be reached from it.

4 4
1 2
1 3
2 4
3 4
2

Hint

Explanation for Sample 1

It can be proven that only vertices 11 and 44 satisfy the requirement.

Since vertex 33 cannot reach vertex 22 and also cannot be reached from vertex 22, vertices 22 and 33 do not satisfy the requirement.

Sample 2

See the attached files gameh2.in and gameh2.ans.

The constraints of this sample are the same as test point 22.

Sample 3

See the attached files gameh3.in and gameh3.ans.

The constraints of this sample are the same as test point 33.

Constraints

This problem has 1010 test points. The test points are not evenly distributed, and the score for each test point is as follows.

Test Point ID Score n=n= m=m=
121\sim 2 55 1010 2020
343\sim 4 100100 10310^3
565\sim 6 10310^3 10410^4
787\sim 8 1515 5×1045\times 10^4 5×1055\times 10^5
9109\sim 10 2020 10610^6 3×1063\times 10^6

For all testdata, it is guaranteed that 10n10610 \le n \le 10^6 and 20m3×10620 \le m \le 3\times 10^6.

For odd-numbered test points, it is guaranteed that the given graph has no cycles with more than 11 distinct vertex.

Hint

The input and output size of this problem is large, so please use fast I/O.

Translated by ChatGPT 5