You are given an undirected graph. You are given an integer n which is the number of nodes in the graph and an array edges, where each edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi.
A connected trio is a set of three nodes where there is an edge between every pair of them.
The degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.
Return theminimum degree of a connected trio in the graph, or-1if the graph has no connected trios.

Input: n =6, edges =[[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]Output: 3Explanation: There is exactly one trio, which is[1,2,3]. The edges that form its degree are bolded in the figure above.

Input: n =7, edges =[[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]Output: 0Explanation: There are exactly three trios:1)[1,4,3]with degree 0.2)[2,5,6]with degree 2.3)[5,6,7]with degree 2.
A connected trio is a triangle in the undirected graph. For each trio, we count the degree of each node and subtract the internal edges to get the degree of the trio. We check all possible trios and keep track of the minimum degree.
classSolution {
publicintminTrioDegree(int n, int[][] edges) {
boolean[][] adj =newboolean[n+1][n+1];
int[] deg =newint[n+1];
for (int[] e : edges) {
adj[e[0]][e[1]]= adj[e[1]][e[0]]=true;
deg[e[0]]++;
deg[e[1]]++;
}
int ans = Integer.MAX_VALUE;
for (int i = 1; i <= n; ++i) {
for (int j = i+1; j <= n; ++j) {
if (!adj[i][j]) continue;
for (int k = j+1; k <= n; ++k) {
if (adj[i][k]&& adj[j][k]) {
int d = deg[i]+ deg[j]+ deg[k]- 6;
ans = Math.min(ans, d);
}
}
}
}
return ans == Integer.MAX_VALUE?-1 : ans;
}
}
classSolution {
funminTrioDegree(n: Int, edges: Array<IntArray>): Int {
val adj = Array(n+1) { BooleanArray(n+1) }
val deg = IntArray(n+1)
for (e in edges) {
adj[e[0]][e[1]] = true adj[e[1]][e[0]] = true deg[e[0]]++ deg[e[1]]++ }
var ans = Int.MAX_VALUE
for (i in1..n) {
for (j in i+1..n) {
if (!adj[i][j]) continuefor (k in j+1..n) {
if (adj[i][k] && adj[j][k]) {
val d = deg[i] + deg[j] + deg[k] - 6 ans = minOf(ans, d)
}
}
}
}
returnif (ans ==Int.MAX_VALUE) -1else ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defminTrioDegree(self, n: int, edges: list[list[int]]) -> int:
adj = [[False]*(n+1) for _ in range(n+1)]
deg = [0]*(n+1)
for u, v in edges:
adj[u][v] = adj[v][u] =True deg[u] +=1 deg[v] +=1 ans = float('inf')
for i in range(1, n+1):
for j in range(i+1, n+1):
ifnot adj[i][j]: continuefor k in range(j+1, n+1):
if adj[i][k] and adj[j][k]:
d = deg[i] + deg[j] + deg[k] -6 ans = min(ans, d)
return-1if ans == float('inf') else ans
impl Solution {
pubfnmin_trio_degree(n: i32, edges: Vec<Vec<i32>>) -> i32 {
let n = n asusize;
letmut adj =vec![vec![false; n+1]; n+1];
letmut deg =vec![0; n+1];
for e in&edges {
adj[e[0] asusize][e[1] asusize] =true;
adj[e[1] asusize][e[0] asusize] =true;
deg[e[0] asusize] +=1;
deg[e[1] asusize] +=1;
}
letmut ans =i32::MAX;
for i in1..=n {
for j in i+1..=n {
if!adj[i][j] { continue; }
for k in j+1..=n {
if adj[i][k] && adj[j][k] {
let d = deg[i] + deg[j] + deg[k] -6;
ans = ans.min(d);
}
}
}
}
if ans ==i32::MAX { -1 } else { ans }
}
}