LC.P1761[一个图中连通三元组的最小度数]

方法一:暴力枚举

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
class Solution {
public int minTrioDegree(int n, int[][] edges) {
boolean[][] g = new boolean[n][n];
int[] deg = new int[n];
for (int[] e : edges) {
int a = e[0] - 1, b = e[1] - 1;
g[a][b] = true;
g[b][a] = true;
++deg[a];
++deg[b];
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (g[i][j]) {
for (int k = j + 1; k < n; ++k) {
if (g[i][k] && g[j][k]) {
ans = Math.min(ans, deg[i] + deg[j] + deg[k] - 6);
}
}
}
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}
  • 时间复杂度:$O(n^3)$
  • 空间复杂度:$O(n^2)$