Solution in C++
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\n- Time 
# 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\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    struct DisjointUnionSets\n    {\n    int *rank,*parent;\n    int x;\n    DisjointUnionSets(int n)\n    {\n        rank=new int[n];\n        parent=new int[n];\n        x=n;\n        for(int i=0;i<n;i++)\n            parent[i]=i;\n    }\n    int find(int x)\n    {\n        if(parent[x]!=x)\n            parent[x]=find(parent[x]);\n        return parent[x];\n    }\n    bool Union(int x,int y)\n    {\n        int xRoot=find(x),yRoot=find(y);\n        if(xRoot == yRoot)\n            return false;\n        if(rank[xRoot] < rank[yRoot])\n            parent[xRoot]=yRoot;\n        else if (rank[xRoot] > rank[yRoot])\n            parent[yRoot]=xRoot;\n        else\n        {\n            parent[yRoot]=xRoot;\n            rank[xRoot]++;\n        }\n        return true;\n    }\n};\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges)\n     {\n        int size=edges.size();\n        vector<pair<int,int>> alice;\n        vector<pair<int,int>> bob;\n        vector<pair<int,int>> both;\n        for(int i=0;i<size;i++)\n        {\n            int temp=edges[i][0];\n            if(temp==1)\n                alice.push_back(make_pair(edges[i][1]-1,edges[i][2]-1));\n            else if(temp==2)\n                bob.push_back(make_pair(edges[i][1]-1,edges[i][2]-1));\n            else\n                both.push_back(make_pair(edges[i][1]-1,edges[i][2]-1));\n        }\n        DisjointUnionSets a(n),b(n);\n        int res=0,componentA=n,componentB=n;\n        for(auto x:both){\n            bool flag1=a.Union(x.first,x.second);\n            bool flag2=b.Union(x.first,x.second);\n            if(flag1)\n                componentA--;\n            if(flag2)\n                componentB--;\n            if(flag1 && flag2)\n                res++;\n        }\n        for(auto x:alice){\n            bool flag=a.Union(x.first,x.second);\n            if(flag)\n            {\n                componentA--;\n                res++;\n            }\n        }\n        for(auto x:bob)\n        {\n            bool flag=b.Union(x.first,x.second);\n            if(flag)\n            {\n                componentB--;\n                res++;\n            }\n        }\n        if(componentA!=1 ||componentB!=1)\n            return -1;\n        return size-res;\n    }\n};\n```
['C++']