2608. Shortest Cycle in a Graph Hard
1/**
2 * [2608] Shortest Cycle in a Graph
3 *
4 * There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1. The edges in the graph are represented by a given 2D integer array edges, where edges[i] = [ui, vi] denotes an edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
5 * Return the length of the shortest cycle in the graph. If no cycle exists, return -1.
6 * A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.
7 *
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;" />
10 * Input: n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
11 * Output: 3
12 * Explanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0
13 *
14 * <strong class="example">Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;" />
16 * Input: n = 4, edges = [[0,1],[0,2]]
17 * Output: -1
18 * Explanation: There are no cycles in this graph.
19 *
20 *
21 * Constraints:
22 *
23 * 2 <= n <= 1000
24 * 1 <= edges.length <= 1000
25 * edges[i].length == 2
26 * 0 <= ui, vi < n
27 * ui != vi
28 * There are no repeated edges.
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/shortest-cycle-in-a-graph/
34// discuss: https://leetcode.com/problems/shortest-cycle-in-a-graph/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn find_shortest_cycle(n: i32, edges: Vec<Vec<i32>>) -> i32 {
40 0
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_2608() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.