#abc245f. F - Endless Walk
F - Endless Walk
Score : points
问题描述
我们有一个简单的有向图 ,包含 个顶点和 条边。顶点被标记为顶点 、顶点 、、顶点 。第 条边()从顶点 指向顶点 。
Takahashi 将从一个顶点开始,并沿一条有向边在图 上重复地从一个顶点移动到另一个顶点。请问图 中有多少个顶点满足以下条件:Takahashi 可以从该顶点开始,并通过精心选择路径无限次地继续旅行?
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
5 5
1 2
2 3
3 4
4 2
4 5
Sample Output 1
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.
Sample Input 2
3 2
1 2
2 1
Sample Output 2
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.
update @ 2024/3/10 10:32:07