Union Find | C++
\nclass Solution {\n    int find_prnt(int node, vector<int> &prnt) {\n        if(node == prnt[node]) return node;\n        return prnt[node] = find_prnt(prnt[no
```\nclass Solution {\n    int find_prnt(int node, vector<int> &prnt) {\n        if(node == prnt[node]) return node;\n        return prnt[node] = find_prnt(prnt[node], prnt);\n    }\n    void unionn(int u, int v, vector<int> &prnt, vector<int> &rank) {\n        int ulp_u = find_prnt(u, prnt), ulp_v = find_prnt(v, prnt);\n\n        if(rank[ulp_u] > rank[ulp_v]) prnt[ulp_v] = ulp_u; \n        else if(rank[ulp_u] < rank[ulp_v]) prnt[ulp_u] = ulp_v;\n        else {\n            prnt[ulp_v] = ulp_u;\n            rank[ulp_u]++;\n        }\n    }\n    int solve(vector<pair<int, int>> &edges, vector<int> &prnt, vector<int> &rank) {\n        int ans = 0;\n        for(auto e : edges) {\n            if(find_prnt(e.first, prnt) != find_prnt(e.second, prnt)) {\n                unionn(e.first, e.second, prnt, rank);\n            }\n            else ans++;\n        }\n        int p = find_prnt(1, prnt);\n        for(int i=1; i<prnt.size(); i++) {\n            if(find_prnt(i, prnt) != p) return -1;\n        }\n        return ans;\n    }\npublic:\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n        vector<int> prnt1(n + 1), rank1(n + 1);\n        vector<int> prnt2(n + 1), rank2(n + 1);\n        for(int i=1; i<=n; i++) prnt1[i] = i, prnt2[i] = i;\n\n        vector<pair<int, int>> alice, bob;\n        int cntAlice = 0, cntBob = 0, ans = 0;\n\n        for(auto e : edges) {\n            if(e[0] == 3) {\n                if(find_prnt(e[1], prnt1) != find_prnt(e[2], prnt1)) {\n                    unionn(e[1], e[2], prnt1, rank1);\n                }\n                else ans++;\n\n                if(find_prnt(e[1], prnt2) != find_prnt(e[2], prnt2)) {\n                    unionn(e[1], e[2], prnt2, rank2);\n                }\n            }\n            else if(e[0] == 2) bob.push_back({e[1], e[2]});\n            else alice.push_back({e[1], e[2]});\n        }\n\n        int a = solve(alice, prnt1, rank1), b = solve(bob, prnt2, rank2);\n        if(a == -1 || b == -1) return -1;\n        return a + b + ans;\n    }\n};\n```
['Union Find', 'C++']