[Python3] - Union Find - Easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\nWith E 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\nWith `E` is number of edges and `V` is number of vertices\n- Time complexity: $$O(ElogE)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(V)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass UnionFind:\n\n    def __init__(self, n: int):\n        self.root = [i for i in range(n)]\n        self.rank = [1 for _ in range(n)]\n        self.group = n\n\n    def find(self, x: int) -> int:\n        if self.root[x] != x: self.root[x] = self.find(self.root[x])\n        return self.root[x]\n\n    def union(self, x: int, y: int) -> None:\n        root_x, root_y = self.find(x), self.find(y)\n        if root_x == root_y: return\n        if self.rank[root_x] > self.rank[root_y]: self.root[root_y] = root_x\n        elif self.rank[root_x] < self.rank[root_y]: self.root[root_x] = root_y\n        else:\n            self.root[root_y] = root_x\n            self.rank[root_x] += 1\n        self.group -= 1\n\n    def are_connected(self, x: int, y: int) -> bool:\n        return self.find(x) == self.find(y)\n\n\nclass Solution:\n    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:\n        uf_a, uf_b = UnionFind(n), UnionFind(n)\n        res = 0\n        edges.sort(reverse=True)\n        for t, u, v in edges:\n            if t == 3:\n                if uf_a.are_connected(u - 1, v - 1) or uf_b.are_connected(u - 1,  v - 1): res += 1\n                else:\n                    uf_a.union(u - 1, v - 1)\n                    uf_b.union(u - 1, v - 1)\n            if t == 2:\n                if uf_b.are_connected(u - 1, v - 1): res += 1\n                else: uf_b.union(u - 1, v - 1)\n            if t == 1:\n                if uf_a.are_connected(u - 1, v - 1): res += 1\n                else: uf_a.union(u - 1, v - 1)\n            \n        return res if uf_a.group == uf_b.group == 1 else -1\n```
['Union Find', 'Graph', 'Sorting', 'Python3']