<p>Alice and Bob have an undirected graph of <code>n</code> nodes and three types of edges:</p>

<ul>
	<li>Type 1: Can be traversed by Alice only.</li>
	<li>Type 2: Can be traversed by Bob only.</li>
	<li>Type 3: Can be traversed by both Alice and Bob.</li>
</ul>

<p>Given an array <code>edges</code> where <code>edges[i] = [type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>]</code> represents a bidirectional edge of type <code>type<sub>i</sub></code> between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.</p>

<p>Return <em>the maximum number of edges you can remove, or return</em> <code>-1</code> <em>if Alice and Bob cannot fully traverse the graph.</em></p>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex1.png" style="width: 179px; height: 191px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex2.png" style="width: 178px; height: 190px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
<strong>Output:</strong> 0
<strong>Explanation: </strong>Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex3.png" style="width: 178px; height: 190px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
<strong>Output:</strong> -1
<b>Explanation: </b>In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it&#39;s impossible to make the graph fully traversable.</pre>

<p>&nbsp;</p>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= edges.length &lt;= min(10<sup>5</sup>, 3 * n * (n - 1) / 2)</code></li>
	<li><code>edges[i].length == 3</code></li>
	<li><code>1 &lt;= type<sub>i</sub> &lt;= 3</code></li>
	<li><code>1 &lt;= u<sub>i</sub> &lt; v<sub>i</sub> &lt;= n</code></li>
	<li>All tuples <code>(type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.</li>
</ul>

"""
Use a union find.
It's best to start with type 3.
Then do type 2 and type 1.

Initially the graph will have n nodes (or components) that are disconnected
First step is to process type 3 edges using a union find.
Then see how many disconnected components are left. If there is one component (the entire graph). Then we are done.

Then we process type 1 and type 2 edges. We return the total no. of edges minus the no. of edges we used of all types.
"""

"""
This function takes in a set of edges, it also takes 2 extra arrays: parent and size.
Parent[u] is the representative of the component u is in.
Size[u] is the size of the component rooted at u.

Union find will return the minimum no. of edges used to make the graph as connected as possible.
It will also modify parent and size.
"""


def union_find(edges, parent, sizes):
    
    def union(u, v):
        u = find(u)
        v = find(v)

        if sizes[v] > sizes[u]:
            u,v = v, u
        
        sizes[u] += sizes[v]
        parent[v] = u
    
    def find(u):
        if u != parent[u]:
            parent[u] = find(parent[u])
        return parent[u]
    
    count = 0

    for u,v in edges:
        if find(u) == find(v): continue
        union(u,v)
        count += 1
    return count



class Solution:
    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
        type3 = []
        type2 = []
        type1 = []
        
        V = n
        E = len(edges)

        for type_, u, v in edges:
            u -= 1
            v -= 1
            if type_ == 3:
                type3.append((u,v))
            elif type_ == 2:
                type2.append((u,v))
            elif type_ == 1:
                type1.append((u,v))
        
        parent = [i for i in range(V)]
        sizes = [1] * V

        type3_count = union_find(type3, parent, sizes)

        alice_parent = parent[:]
        alice_sizes = sizes[:]

        bob_parent = parent[:]
        bob_sizes = sizes[:]

        type2_count = union_find(type1, alice_parent, alice_sizes)
        type1_count = union_find(type2,  bob_parent, bob_sizes)

        if max(bob_sizes) < V or max(alice_sizes) < V:
            return -1
        
        return E - (type3_count + type2_count + type1_count)




59000