#abc311c. C - Find it!(WAITING SPJ)
C - Find it!(WAITING SPJ)
Score : points
问题描述
存在一个有向图,包含 个顶点和 条边。 第 条边从顶点 指向顶点 。(约束条件保证了 。) 寻找一个不含重复顶点的有向循环。 在本问题的约束条件下可以证明存在解。
注释
顶点序列 被称为有向循环,当满足以下所有条件时:
- 存在从顶点 到顶点 的边。
- 存在从顶点 到顶点 的边。
- 若 ,则 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a directed graph with vertices and edges. The -th edge goes from vertex to vertex . (The constraints guarantee that .) Find a directed cycle without the same vertex appearing multiple times. It can be shown that a solution exists under the constraints of this problem.
Notes
The sequence of vertices is called a directed cycle when all of the following conditions are satisfied:
- The edge from vertex to vertex exists.
- The edge from vertex to vertex exists.
- If , then .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print a solution in the following format:
is the number of vertices, and is the -th vertex in the directed cycle.
The following conditions must be satisfied:
- ( )
- ( )
If multiple solutions exist, any of them will be accepted.
Sample Input 1
7
6 7 2 1 3 4 5
Sample Output 1
4
7 5 3 2
$7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7$ is indeed a directed cycle.
Here is the graph corresponding to this input:
Here are other acceptable outputs:
4
2 7 5 3
3
4 1 6
Note that the graph may not be connected.
Sample Input 2
2
2 1
Sample Output 2
2
1 2
This case contains both of the edges and . In this case, is indeed a directed cycle.
Here is the graph corresponding to this input, where represents the existence of both and :
Sample Input 3
8
3 7 4 7 3 3 8 2
Sample Output 3
3
2 7 8
Here is the graph corresponding to this input:
update @ 2024/3/10 08:50:26