<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>

class DisjointSet():
    def __init__(self, n):
        self.parents = [x for x in range(n)]
        self.weights = [1 for _ in range(n)]
        self.num_components = n
    
    def isConnected(self, u, v):
        return self.findRoot(u) == self.findRoot(v)
    
    def findRoot(self, u):
        tmp = u
        while self.parents[tmp] != tmp:
            tmp = self.parents[self.parents[tmp]]
        return tmp
    
    def union(self, u, v):
        root_a, root_b = self.findRoot(u), self.findRoot(v)
        if root_a == root_b:
            return
        if self.weights[root_a] > self.weights[root_b]:
            self.parents[root_b] = root_a
            self.weights[root_a] += self.weights[root_b]
            self.weights[root_b] = self.weights[root_a]
        else:
            self.parents[root_a] = root_b
            self.weights[root_b] += self.weights[root_a]
            self.weights[root_a] = self.weights[root_b]
        self.num_components -= 1
    
    def getNumConnectedComponents(self):
        return self.num_components

class Solution:
    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
        # 2 disjoint sets, one for Alice, one for Bob
        # For type 1 edges, check if nodes connected in Alice's DS
        # For type 2 edges, check if nodes connected in Bob's DS
        # For type 3, check both
        edges.sort(reverse=True)
        alice = DisjointSet(n)
        bob = DisjointSet(n)
        count = 0
        for edge_type, u, v in edges:
            if edge_type == 1:
                if alice.isConnected(u - 1, v - 1):
                    count += 1
                    continue
                alice.union(u - 1, v - 1)
            elif edge_type == 2:
                if bob.isConnected(u - 1, v - 1):
                    count += 1
                    continue
                bob.union(u - 1, v - 1)
            elif edge_type == 3:
                if alice.isConnected(u - 1, v - 1) and bob.isConnected(u - 1, v - 1):
                    count += 1
                    continue
                alice.union(u - 1, v - 1)
                bob.union(u - 1, v - 1)
        a_root = alice.findRoot(0)
        b_root = bob.findRoot(0)

        if not alice.getNumConnectedComponents() == 1 or not bob.getNumConnectedComponents() == 1:
            return -1
        return count
                
54400