[Python] Union Find
Intuition\nAdd Type3 first, then check Type 1 and Type 2.\n\n\n# Explanation\nGo through all edges of type 3 (Alice and Bob)\nIf not necessary to add, increment
# **Intuition**\nAdd Type3 first, then check Type 1 and Type 2.\n<br>\n\n# **Explanation**\nGo through all edges of type 3 (Alice and Bob)\nIf not necessary to add, increment `res`.\nOtherwith increment `e1` and `e2`.\n\nGo through all edges of type 1 (Alice)\nIf not necessary to add, increment `res`.\nOtherwith increment `e1`.\n\nGo through all edges of type 2 (Bob)\nIf not necessary to add, increment `res`.\nOtherwith increment `e2`.\n\nIf Alice\'s\'graph is connected, `e1 == n - 1` should valid.\nIf Bob\'s graph is connected, `e2 == n - 1` should valid.\nIn this case we return `res`,\notherwise return `-1`.\n<br>\n\n# Complexity\nTime O(E), if union find with compression and rank\nSpace O(E)\n<br>\n\n**Python:**\n```py\n    def maxNumEdgesToRemove(self, n, edges):\n        # Union find\n        def find(i):\n            if i != root[i]:\n                root[i] = find(root[i])\n            return root[i]\n\n        def uni(x, y):\n            x, y = find(x), find(y)\n            if x == y: return 0\n            root[x] = y\n            return 1\n\n        res = e1 = e2 = 0\n\n        # Alice and Bob\n        root = range(n + 1)\n        for t, i, j in edges:\n            if t == 3:\n                if uni(i, j):\n                    e1 += 1\n                    e2 += 1\n                else:\n                    res += 1\n        root0 = root[:]\n\n        # only Alice\n        for t, i, j in edges:\n            if t == 1:\n                if uni(i, j):\n                    e1 += 1\n                else:\n                    res += 1\n\n        # only Bob\n        root = root0\n        for t, i, j in edges:\n            if t == 2:\n                if uni(i, j):\n                    e2 += 1\n                else:\n                    res += 1\n\n        return res if e1 == e2 == n - 1 else -1\n```\n
[]