atcoder#ABC245F. [ABC245F] Endless Walk
[ABC245F] Endless Walk
Score : points
Problem Statement
We have a simple directed graph with vertices and edges. The vertices are labeled as Vertex , Vertex , , Vertex . The -th edge goes from Vertex to Vertex .
Takahashi will start at a vertex and repeatedly travel on from one vertex to another along a directed edge. How many vertices of have the following condition: Takahashi can start at that vertex and continue traveling indefinitely by carefully choosing the path?
Constraints
- if .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
5 5
1 2
2 3
3 4
4 2
4 5
4
When starting at Vertex , Takahashi can continue traveling indefinitely: The same goes when starting at Vertex or Vertex . From Vertex , he can first go to Vertex and then continue traveling indefinitely again. On the other hand, from Vertex , he cannot move at all.
Thus, four vertices ―Vertex , , , and ― satisfy the conditions, so should be printed.
3 2
1 2
2 1
2
Note that, in a simple directed graph, there may be two edges in opposite directions between the same pair of vertices. Additionally, may not be connected.