EIUCIRCUIT - Find a circuit
Xem dạng PDF
Gửi bài giải
Điểm:
1,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
256M
Input:
stdin
Output:
stdout
Nguồn bài:
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
Given a simple undirected graph, check whether it contains a circuit. If a circuit exists, print the nodes in the circuit in the same order they appear in the circuit. If there are multiple circuits, print any one of them.
Input
- The first line contains two positive integers: ~n~, the number of vertices ~(0 < n < 10^5)~, and ~m~, the number of edges ~(m \le 2 \times 10^5)~ The next ~m~ lines each contains two positive integers ~u, v~ ~(0 < u, v ≤ 10^5)~, representing an edge between nodes ~u~ and ~v~.
Output
- Print nodes in the circuit. If no circuit exists, print
-1
Example Input 1
4 4
1 2
2 3
3 1
3 4
Example Output 1
1 2 3
Example Input 2
6 6
1 2
2 3
3 1
4 5
5 6
6 4
Example Output 2
5 6 4
Example Input 3
4 3
1 2
2 3
3 4
Example Output 3
-1
Note
- In the first example, the graph contains a single circuit. Any of the following outputs is acceptable:
1 2 3,2 3 1,3 1 2,3 2 1,2 1 3, or1 3 2
- In the second example, the graph contains two distinct circuits.
Bình luận