Alice and Bob have an undirected graph of n nodes and 3 types of edges:
Type 1: Can be traversed by Alice only.
Type 2: Can be traversed by Bob only.
Type 3: Can by traversed by both Alice and Bob.
Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, 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.
Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.
Example 1:
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: 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.
Example 2:
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
Example 3:
Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.
Constraints:
1 <= n <= 10^5
1 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)
edges[i].length == 3
1 <= edges[i][0] <= 3
1 <= edges[i][1] < edges[i][2] <= n
All tuples (typei, ui, vi) are distinct.
class DisjointSet:
    def __init__(self, number_of_sites):
        self.parent = [i for i in range(number_of_sites+1)]
        self.children_site_count = [1 for _ in range(number_of_sites+1)]
        self.component_count = number_of_sites

    def find_root(self, site):
        root = site
        while root != self.parent[root]:
            root = self.parent[root]
        while site != root:
            site, self.parent[site] = self.parent[site], root
        return root

    def is_connected(self, site_1, site_2):
        return self.find_root(site_1) == self.find_root(site_2)

    def union(self, site_1, site_2):
        site_1_root = self.find_root(site_1)
        site_2_root = self.find_root(site_2)
        if site_1_root == site_2_root:
            return False

        if self.children_site_count[site_1_root] < self.children_site_count[site_2_root]:
            self.parent[site_1_root] = site_2_root
            self.children_site_count[site_2_root] += self.children_site_count[
                site_1_root]
        else:
            self.parent[site_2_root] = site_1_root
            self.children_site_count[site_1_root] += self.children_site_count[
                site_2_root]
        self.component_count -= 1
        return True


class Solution:
    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
        alice_disjoint_set = DisjointSet(n)
        bob_disjoint_set = DisjointSet(n)

        TYPE_OF_COMMON_EDGES = 3
        TYPE_OF_ALICE_EDGES = 1
        TYPE_OF_BOB_EDGES = 2

        common_edges = filter(lambda edge: edge[0] == TYPE_OF_COMMON_EDGES, edges)
        alice_edges = filter(lambda edge: edge[0] == TYPE_OF_ALICE_EDGES, edges)
        bob_edges = filter(lambda edge: edge[0] == TYPE_OF_BOB_EDGES, edges)

        redundant = 0
        for _, u, v in common_edges:
            if (not alice_disjoint_set.union(u, v)) or (not bob_disjoint_set.union(u, v)):
                redundant += 1

        for _, u, v in bob_edges:
            if not bob_disjoint_set.union(u,v):
                redundant += 1
                
        for _, u, v in alice_edges:
            if not alice_disjoint_set.union(u, v):
                redundant += 1
        
        return redundant if alice_disjoint_set.component_count == 1 and bob_disjoint_set.component_count == 1 else -1
class DisjointSet:
    def __init__(self, number_of_sites):
        self.parent = [i for i in range(number_of_sites+1)]
        self.children_site_count = [1 for _ in range(number_of_sites+1)]
        self.component_count = number_of_sites

    def find_root(self, site):
        root = site
        while root != self.parent[root]:
            root = self.parent[root]
        while site != root:
            site, self.parent[site] = self.parent[site], root
        return root

    def is_connected(self, site_1, site_2):
        return self.find_root(site_1) == self.find_root(site_2)

    def union(self, site_1, site_2):
        site_1_root = self.find_root(site_1)
        site_2_root = self.find_root(site_2)
        if site_1_root == site_2_root:
            return False

        if self.children_site_count[site_1_root] < self.children_site_count[site_2_root]:
            self.parent[site_1_root] = site_2_root
            self.children_site_count[site_2_root] += self.children_site_count[
                site_1_root]
        else:
            self.parent[site_2_root] = site_1_root
            self.children_site_count[site_1_root] += self.children_site_count[
                site_2_root]
        self.component_count -= 1
        return True


class Solution:
    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
        alice_disjoint_set = DisjointSet(n)
        bob_disjoint_set = DisjointSet(n)

        TYPE_OF_COMMON_EDGES = 3
        TYPE_OF_ALICE_EDGES = 1
        TYPE_OF_BOB_EDGES = 2

        common_edges = filter(lambda edge: edge[0] == TYPE_OF_COMMON_EDGES, edges)
        alice_edges = filter(lambda edge: edge[0] == TYPE_OF_ALICE_EDGES, edges)
        bob_edges = filter(lambda edge: edge[0] == TYPE_OF_BOB_EDGES, edges)

        redundant = 0
        for _, u, v in common_edges:
            if (not alice_disjoint_set.union(u, v)) or (not bob_disjoint_set.union(u, v)):
                redundant += 1

        for _, u, v in bob_edges:
            if not bob_disjoint_set.union(u,v):
                redundant += 1
                
        for _, u, v in alice_edges:
            if not alice_disjoint_set.union(u, v):
                redundant += 1
        
        return redundant if alice_disjoint_set.component_count == 1 and bob_disjoint_set.component_count == 1 else -1