Simplest Python UF solution
\n# Code\n\nclass DSU():\n    def __init__(self, n):\n        self.par = list(range(n))\n        self.rank = [1] * n\n        self.size = 1\n    \n    def find(
\n# Code\n```\nclass DSU():\n    def __init__(self, n):\n        self.par = list(range(n))\n        self.rank = [1] * n\n        self.size = 1\n    \n    def find(self, u):\n        if u != self.par[u]:\n            self.par[u] = self.find(self.par[u])\n        return self.par[u]\n    \n    def union(self, u, v):\n        uu, vv = self.find(u), self.find(v)\n        if uu == vv:\n            return False\n        if self.rank[uu] > self.rank[vv]:\n            self.par[vv] = self.par[self.par[vv]]\n            self.par[vv] = uu\n        elif self.rank[vv] > self.par[uu]:\n            self.par[uu] = self.par[self.par[uu]]\n            self.par[uu] = vv\n        else:\n            self.par[uu] = vv\n            self.rank[vv] += 1\n        self.size += 1\n        return True\n\nclass Solution:\n    def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:\n        uf1, uf2, ans = DSU(n), DSU(n), 0\n\n        for t, u, v in edges:\n            if t == 3 and (not uf1.union(u-1, v-1) or not uf2.union(u-1, v-1)):\n                ans += 1\n        for t, u, v in edges:\n            if t == 1 and not uf1.union(u-1, v-1):\n                ans += 1\n            elif t == 2 and not uf2.union(u-1, v-1):\n                ans += 1\n        return ans if uf1.size == n and uf2.size == n else -1\n```
['Python3']