Python, Union-Find  two-pass
\nclass Solution(object):\n    def maxNumEdgesToRemove(self, n, edges):\n\n        ufa = UnionFind(n) # Graph for Alice\n        ufb = UnionFind(n) # Graph for 
```\nclass Solution(object):\n    def maxNumEdgesToRemove(self, n, edges):\n\n        ufa = UnionFind(n) # Graph for Alice\n        ufb = UnionFind(n) # Graph for Bob\n        cnt = 0 # number of removable edges\n        \n        for x, y, z in edges:\n            if x == 3:\n                flag1 = ufa.union(y, z)\n                flag2 = ufb.union(y, z)\n                if not flag1 or not flag2: cnt +=1\n\n        for x, y, z in edges:\n            if x == 1:\n                flag = ufa.union(y, z)\n                if not flag: cnt += 1\n            if x == 2:\n                flag = ufb.union(y, z)\n                if not flag: cnt += 1\n\n        return cnt if ufa.groups == 1 and ufb.groups == 1 else -1\n            \n        \nclass UnionFind():\n    def __init__(self, n):\n        self.parents = {i:i for i in range(1, n+1)}\n        self.groups = n\n\n    def find(self, x):\n        if self.parents[x] == x:\n            return x\n        else:\n            self.parents[x] = self.find(self.parents[x])\n            return self.parents[x]\n\n    def union(self, x, y):\n        x = self.find(x)\n        y = self.find(y)\n\n        if x == y:\n            return False\n\n        self.parents[y] = x\n        self.groups -= 1\n        return True\n```
['Union Find', 'Python']