<p>You are given an undirected graph. You are given an integer <code>n</code> which is the number of nodes in the graph and an array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an undirected edge between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>

<p>A <strong>connected trio</strong> is a set of <strong>three</strong> nodes where there is an edge between <b>every</b> pair of them.</p>

<p>The <strong>degree of a connected trio</strong> is the number of edges where one endpoint is in the trio, and the other is not.</p>

<p>Return <em>the <strong>minimum</strong> degree of a connected trio in the graph, or</em> <code>-1</code> <em>if the graph has no connected trios.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios1.png" style="width: 388px; height: 164px;" />
<pre>
<strong>Input:</strong> n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios2.png" style="width: 388px; height: 164px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 400</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>1 &lt;= edges.length &lt;= n * (n-1) / 2</code></li>
	<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
	<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
</ul>

class Solution:
    def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
        # Create an adjacency matrix to check the existence of an edge in O(1) time.
        adj_matrix = [[False] * (n + 1) for _ in range(n + 1)]

        # Create an adjacency list to store neighbors of each node for efficient traversal.
        adj_list = [[] for _ in range(n + 1)]

        # Array to keep track of the degree of each node.
        degree = [0] * (n + 1)

        # Fill the adjacency matrix, adjacency list, and degree array.
        for u, v in edges:
            adj_matrix[u][v] = True  # Mark the edge (u, v) as present in the matrix.
            adj_matrix[v][u] = True  # Mark the edge (v, u) as present (since undirected).
            adj_list[u].append(v)    # Add v to u's neighbor list.
            adj_list[v].append(u)    # Add u to v's neighbor list.
            degree[u] += 1           # Increment the degree of node u.
            degree[v] += 1           # Increment the degree of node v.

        # Initialize the minimum degree of a connected trio to infinity.
        min_degree = float('inf')

        # Iterate over each node to find connected trios.
        for u in range(1, n + 1):
            # Check all pairs of neighbors of node u.
            for v in adj_list[u]:
                for w in adj_list[v]:
                    # Check if w is a neighbor of u, forming a trio [u, v, w].
                    if u != w and adj_matrix[u][w]:
                        # Calculate the degree of the connected trio.
                        current_degree = degree[u] + degree[v] + degree[w] - 6
                        # Update the minimum degree if the current trio has a smaller degree.
                        min_degree = min(min_degree, current_degree)

        # If no trio was found, return -1. Otherwise, return the minimum degree.
        return min_degree if min_degree != float('inf') else -1
38400