题目描述
现有一个含 n
个顶点的 双向 图,每个顶点按从 0
到 n - 1
标记。图中的边由二维整数数组 edges
表示,其中 edges[i] = [ui, vi]
表示顶点 ui
和 vi
之间存在一条边。每对顶点最多通过一条边连接,并且不存在与自身相连的顶点。
返回图中 最短 环的长度。如果不存在环,则返回 -1
。
环 是指以同一节点开始和结束,并且路径中的每条边仅使用一次。
示例 1:
输入:n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
输出:3
解释:长度最小的循环是:0 -> 1 -> 2 -> 0
示例 2:
输入:n = 4, edges = [[0,1],[0,2]]
输出:-1
解释:图中不存在循环
提示:
2 <= n <= 1000
1 <= edges.length <= 1000
edges[i].length == 2
0 <= ui, vi < n
ui != vi
- 不存在重复的边
解法
方法一:枚举删除的边 + BFS
我们先根据数组 $edges$ 构建出邻接表 $g$,其中 $g[u]$ 表示顶点 $u$ 的所有邻接点。
接下来,我们枚举双向边 $(u, v)$,如果删除该边后,从顶点 $u$ 到顶点 $v$ 的路径依然存在,则包含该边的最短环的长度为 $dist[v] + 1$,其中 $dist[v]$ 表示从顶点 $u$ 到顶点 $v$ 的最短路径长度。我们取所有这样的环的最小值即可。
时间复杂度 $O(m^2)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为数组 $edges$ 的长度以及顶点数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | class Solution:
def findShortestCycle(self, n: int, edges: List[List[int]]) -> int:
def bfs(u: int, v: int) -> int:
dist = [inf] * n
dist[u] = 0
q = deque([u])
while q:
i = q.popleft()
for j in g[i]:
if (i, j) != (u, v) and (j, i) != (u, v) and dist[j] == inf:
dist[j] = dist[i] + 1
q.append(j)
return dist[v] + 1
g = defaultdict(set)
for u, v in edges:
g[u].add(v)
g[v].add(u)
ans = min(bfs(u, v) for u, v in edges)
return ans if ans < inf else -1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | class Solution {
private List<Integer>[] g;
private final int inf = 1 << 30;
public int findShortestCycle(int n, int[][] edges) {
g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var e : edges) {
int u = e[0], v = e[1];
g[u].add(v);
g[v].add(u);
}
int ans = inf;
for (var e : edges) {
int u = e[0], v = e[1];
ans = Math.min(ans, bfs(u, v));
}
return ans < inf ? ans : -1;
}
private int bfs(int u, int v) {
int[] dist = new int[g.length];
Arrays.fill(dist, inf);
dist[u] = 0;
Deque<Integer> q = new ArrayDeque<>();
q.offer(u);
while (!q.isEmpty()) {
int i = q.poll();
for (int j : g[i]) {
if ((i == u && j == v) || (i == v && j == u) || dist[j] != inf) {
continue;
}
dist[j] = dist[i] + 1;
q.offer(j);
}
}
return dist[v] + 1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 | class Solution {
public:
int findShortestCycle(int n, vector<vector<int>>& edges) {
vector<vector<int>> g(n);
for (auto& e : edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
const int inf = 1 << 30;
auto bfs = [&](int u, int v) -> int {
int dist[n];
fill(dist, dist + n, inf);
dist[u] = 0;
queue<int> q{{u}};
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) {
if ((i == u && j == v) || (i == v && j == u) || dist[j] != inf) {
continue;
}
dist[j] = dist[i] + 1;
q.push(j);
}
}
return dist[v] + 1;
};
int ans = inf;
for (auto& e : edges) {
int u = e[0], v = e[1];
ans = min(ans, bfs(u, v));
}
return ans < inf ? ans : -1;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 | func findShortestCycle(n int, edges [][]int) int {
g := make([][]int, n)
for _, e := range edges {
u, v := e[0], e[1]
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
const inf = 1 << 30
bfs := func(u, v int) int {
dist := make([]int, n)
for i := range dist {
dist[i] = inf
}
dist[u] = 0
q := []int{u}
for len(q) > 0 {
i := q[0]
q = q[1:]
for _, j := range g[i] {
if (i == u && j == v) || (i == v && j == u) || dist[j] != inf {
continue
}
dist[j] = dist[i] + 1
q = append(q, j)
}
}
return dist[v] + 1
}
ans := inf
for _, e := range edges {
u, v := e[0], e[1]
ans = min(ans, bfs(u, v))
}
if ans < inf {
return ans
}
return -1
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | function findShortestCycle(n: number, edges: number[][]): number {
const g: number[][] = new Array(n).fill(0).map(() => []);
for (const [u, v] of edges) {
g[u].push(v);
g[v].push(u);
}
const inf = 1 << 30;
let ans = inf;
const bfs = (u: number, v: number) => {
const dist: number[] = new Array(n).fill(inf);
dist[u] = 0;
const q: number[] = [u];
while (q.length) {
const i = q.shift()!;
for (const j of g[i]) {
if ((i == u && j == v) || (i == v && j == u) || dist[j] != inf) {
continue;
}
dist[j] = dist[i] + 1;
q.push(j);
}
}
return 1 + dist[v];
};
for (const [u, v] of edges) {
ans = Math.min(ans, bfs(u, v));
}
return ans < inf ? ans : -1;
}
|
方法二:枚举点 + BFS
与方法一类似,我们先根据数组 $edges$ 构建出邻接表 $g$,其中 $g[u]$ 表示顶点 $u$ 的所有邻接点。
接下来,我们枚举顶点 $u$,如果从顶点 $u$ 出发,有两条路径都到达了顶点 $v$,说明当前找到了一个环,长度为两条路径的长度之和。我们取所有这样的环的最小值即可。
时间复杂度 $O(m \times n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为数组 $edges$ 的长度以及顶点数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | class Solution:
def findShortestCycle(self, n: int, edges: List[List[int]]) -> int:
def bfs(u: int) -> int:
dist = [-1] * n
dist[u] = 0
q = deque([(u, -1)])
ans = inf
while q:
u, fa = q.popleft()
for v in g[u]:
if dist[v] < 0:
dist[v] = dist[u] + 1
q.append((v, u))
elif v != fa:
ans = min(ans, dist[u] + dist[v] + 1)
return ans
g = defaultdict(list)
for u, v in edges:
g[u].append(v)
g[v].append(u)
ans = min(bfs(i) for i in range(n))
return ans if ans < inf else -1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | class Solution {
private List<Integer>[] g;
private final int inf = 1 << 30;
public int findShortestCycle(int n, int[][] edges) {
g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var e : edges) {
int u = e[0], v = e[1];
g[u].add(v);
g[v].add(u);
}
int ans = inf;
for (int i = 0; i < n; ++i) {
ans = Math.min(ans, bfs(i));
}
return ans < inf ? ans : -1;
}
private int bfs(int u) {
int[] dist = new int[g.length];
Arrays.fill(dist, -1);
dist[u] = 0;
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {u, -1});
int ans = inf;
while (!q.isEmpty()) {
var p = q.poll();
u = p[0];
int fa = p[1];
for (int v : g[u]) {
if (dist[v] < 0) {
dist[v] = dist[u] + 1;
q.offer(new int[] {v, u});
} else if (v != fa) {
ans = Math.min(ans, dist[u] + dist[v] + 1);
}
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 | class Solution {
public:
int findShortestCycle(int n, vector<vector<int>>& edges) {
vector<vector<int>> g(n);
for (auto& e : edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
const int inf = 1 << 30;
auto bfs = [&](int u) -> int {
int dist[n];
memset(dist, -1, sizeof(dist));
dist[u] = 0;
queue<pair<int, int>> q;
q.emplace(u, -1);
int ans = inf;
while (!q.empty()) {
auto p = q.front();
u = p.first;
int fa = p.second;
q.pop();
for (int v : g[u]) {
if (dist[v] < 0) {
dist[v] = dist[u] + 1;
q.emplace(v, u);
} else if (v != fa) {
ans = min(ans, dist[u] + dist[v] + 1);
}
}
}
return ans;
};
int ans = inf;
for (int i = 0; i < n; ++i) {
ans = min(ans, bfs(i));
}
return ans < inf ? ans : -1;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | func findShortestCycle(n int, edges [][]int) int {
g := make([][]int, n)
for _, e := range edges {
u, v := e[0], e[1]
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
const inf = 1 << 30
bfs := func(u int) int {
dist := make([]int, n)
for i := range dist {
dist[i] = -1
}
dist[u] = 0
q := [][2]int{{u, -1}}
ans := inf
for len(q) > 0 {
p := q[0]
u = p[0]
fa := p[1]
q = q[1:]
for _, v := range g[u] {
if dist[v] < 0 {
dist[v] = dist[u] + 1
q = append(q, [2]int{v, u})
} else if v != fa {
ans = min(ans, dist[u]+dist[v]+1)
}
}
}
return ans
}
ans := inf
for i := 0; i < n; i++ {
ans = min(ans, bfs(i))
}
if ans < inf {
return ans
}
return -1
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | function findShortestCycle(n: number, edges: number[][]): number {
const g: number[][] = new Array(n).fill(0).map(() => []);
for (const [u, v] of edges) {
g[u].push(v);
g[v].push(u);
}
const inf = 1 << 30;
let ans = inf;
const bfs = (u: number) => {
const dist: number[] = new Array(n).fill(-1);
dist[u] = 0;
const q: number[][] = [[u, -1]];
let ans = inf;
while (q.length) {
const p = q.shift()!;
u = p[0];
const fa = p[1];
for (const v of g[u]) {
if (dist[v] < 0) {
dist[v] = dist[u] + 1;
q.push([v, u]);
} else if (v !== fa) {
ans = Math.min(ans, dist[u] + dist[v] + 1);
}
}
}
return ans;
};
for (let i = 0; i < n; ++i) {
ans = Math.min(ans, bfs(i));
}
return ans < inf ? ans : -1;
}
|