[ C++ ] [ Union Find ] [ Implementation ]
Code\n\nclass UnionFind {\n    vector<int> representative;\n    vector<int> componentSize;\n    int components;\n    \npublic:\n    UnionFind(int n) {\n        
# Code\n```\nclass UnionFind {\n    vector<int> representative;\n    vector<int> componentSize;\n    int components;\n    \npublic:\n    UnionFind(int n) {\n        components = n;\n        for (int i = 0; i <= n; i++) {\n            representative.push_back(i);\n            componentSize.push_back(1);\n        }\n    }\n    \n    int findRepresentative(int x) {\n        if (representative[x] == x) {\n            return x;\n        }\n        return representative[x] = findRepresentative(representative[x]);\n    }\n    \n    int performUnion(int x, int y) {       \n        x = findRepresentative(x); y = findRepresentative(y);\n        \n        if (x == y) {\n            return 0;\n        }\n        \n        if (componentSize[x] > componentSize[y]) {\n            componentSize[x] += componentSize[y];\n            representative[y] = x;\n        } else {\n            componentSize[y] += componentSize[x];\n            representative[x] = y;\n        }\n        \n        components--;\n        return 1;\n    }\n\n    bool isConnected() {\n        return components == 1;\n    }\n};\n\nclass Solution {\npublic:\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n        UnionFind Alice(n), Bob(n);\n\n        int edgesRequired = 0;\n        for (vector<int>& edge : edges) {\n            if (edge[0] == 3) {\n                edgesRequired += (Alice.performUnion(edge[1], edge[2]) | Bob.performUnion(edge[1], edge[2]));\n            }\n        }\n\n        for (vector<int>& edge : edges) {\n            if (edge[0] == 1) {\n                edgesRequired += Alice.performUnion(edge[1], edge[2]);\n            } else if (edge[0] == 2) {\n                edgesRequired += Bob.performUnion(edge[1], edge[2]);\n            }\n        }\n\n        if (Alice.isConnected() && Bob.isConnected()) {\n            return edges.size() - edgesRequired;\n        }\n        \n        return -1;\n    }\n};\n```
['C++']