<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> vertices, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>

<p>Return <em>the number of <strong>complete connected components</strong> of the graph</em>.</p>

<p>A <strong>connected component</strong> is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.</p>

<p>A connected component is said to be <b>complete</b> if there exists an edge between every pair of its vertices.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png" style="width: 671px; height: 270px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> From the picture above, one can see that all of the components of this graph are complete.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png" style="width: 671px; height: 270px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The component containing vertices 0, 1, and 2 is complete since there is an edge between every pair of two vertices. On the other hand, the component containing vertices 3, 4, and 5 is not complete since there is no edge between vertices 4 and 5. Thus, the number of complete components in this graph is 1.
</pre>

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

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

class Solution:
    def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int:
        # not just union find, ALL vertices must be connected to EACHOTHER.

        # build up unions.
        # afterwards, for each union, check every vertex inside them
        # and check that their adjacency list size == unions size - 1.

        parent = list(range(n))
        size = [1] * n

        def find(num):
            if parent[num] == num:
                return num
            else:
                parent[num] = find(parent[num])
                return parent[num]
        def union(n1, n2):
            p1 = find(n1)
            p2 = find(n2)
            if p1 == p2:
                return False
            else:
                if size[p1] > size[p2]:
                    parent[p2] = p1
                    size[p1] += size[p2]
                else:
                    parent[p1] = p2
                    size[p2] += size[p1]
                return True
        
        adj = {}
        for [fr, to] in edges:
            if fr not in adj:
                adj[fr] = []
            if to not in adj:
                adj[to] = []
            adj[fr].append(to)
            adj[to].append(fr)

        for num in range(n):
            if num not in adj:
                adj[num] = []
            for other in adj[num]:
                union(num, other)

        # Get COMPLETE CONNECTED.
        # Meaning all nodes within one group all have len(adj) == size[p]-1.
        # We are checking for ALL of this to be true under one parent.

        parent_to_count = {}

        # Fully connected islands should have a total edges of nodes * (nodes-1)
        for num in range(n):
            p = find(num)
            if not p in parent_to_count:
                parent_to_count[p] = 0
            parent_to_count[p] += len(adj[num])

        out = 0
        for p, count in parent_to_count.items():
            if count == size[p] * (size[p]-1):
                out += 1
        
        return out







                

625