#abc376d. D - Cycle
D - Cycle
Score : points
问题陈述
有一个简单的有向图,包含个顶点,编号从到,以及条边。第条边()是从顶点指向顶点的有向边。 判断是否存在包含顶点的环,如果存在,找出这样的环中边数最少的环。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There is a simple directed graph with vertices numbered from to and edges. The -th edge is a directed edge from vertex to vertex .
Determine whether there exists a cycle that contains vertex , and if it exists, find the minimum number of edges among such cycles.
Constraints
- $1 \leq M \leq \min \left( \frac{N(N-1)}{2},\ 2 \times 10^5 \right)$
- and , if .
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If there exists a cycle that contains vertex , print the minimum number of edges among such cycles. Otherwise, print -1
.
Sample Input 1
3 3
1 2
2 3
3 1
Sample Output 1
3
Vertex vertex vertex vertex is a cycle with three edges, and this is the only cycle that contains vertex .
Sample Input 2
3 2
1 2
2 3
Sample Output 2
-1
Sample Input 3
6 9
6 1
1 5
2 6
2 1
3 6
4 2
6 4
3 5
5 4
Sample Output 3
4