Easiest Code 🔥✅ Using Disjoint Set Data Structure
\tclass Solution {\n\tpublic:\n\n\tclass Disjoint\n\t{\n\tpublic:\n\t\tvector parent;\n\t\tvector size;\n\n\t\tDisjoint(int n)\n\t\t{\n\t\t\tparent.resize(n+1);
\tclass Solution {\n\tpublic:\n\n\tclass Disjoint\n\t{\n\tpublic:\n\t\tvector<int> parent;\n\t\tvector<int> size;\n\n\t\tDisjoint(int n)\n\t\t{\n\t\t\tparent.resize(n+1);\n\t\t\tsize.resize(n+1, 1);\n\t\t\tfor(int i = 0; i <= n; i++)\n\t\t\t{\n\t\t\t\tparent[i] = i;\n\t\t\t}\n\t\t}\n\n\t\tint findParent(int u)\n\t\t{\n\t\t\tif(u == parent[u])\n\t\t\t\treturn u;\n\n\t\t\treturn parent[u] = findParent(parent[u]);\n\t\t}\n\n\n\t\tbool unionBySize(int u, int v)\n\t\t{\n\t\t\tint ultp_u = findParent(u);\n\t\t\tint ultp_v = findParent(v);\n\n\t\t\tif(ultp_u == ultp_v)\n\t\t\t\treturn false;\n\n\t\t\telse if (size[ultp_u] < size[ultp_v])\n\t\t\t{\n\t\t\t\tparent[ultp_u] = ultp_v;\n\t\t\t\tsize[ultp_v] += size[ultp_u];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tparent[ultp_v] = ultp_u;\n\t\t\t\tsize[ultp_u] += size[ultp_v];\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t};\n\n\t\tbool static cmp(vector<int> &v1, vector<int> &v2)\n\t\t{\n\t\t\treturn v1[0] > v2[0];\n\t\t}\n\n\n\t\tint maxNumEdgesToRemove(int n, vector<vector<int>>& e) \n\t\t{\n\t\t\tDisjoint bob(n);\n\t\t\tDisjoint alice(n);\n\n\n\t\t\tsort(e.begin(), e.end(), cmp);\n\t\t\tint count = 0;\n\t\t\tint bs = 0;\n\t\t\tint as = 0;\n\t\t\tfor(int i = 0; i < e.size(); i++)\n\t\t\t{\n\t\t\t\tint u = e[i][1];\n\t\t\t\tint v = e[i][2];\n\t\t\t\tint t = e[i][0];\n\n\t\t\t\tif(t == 3)\n\t\t\t\t{\n\t\t\t\t\tbool b1 = bob.unionBySize(u, v);\n\t\t\t\t\tbool b2 = alice.unionBySize(u, v);\n\t\t\t\t\tif(b1 == false && b1 == false)\n\t\t\t\t\t{\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t\tif(b1 == true)\n\t\t\t\t\t\tbs++;\n\t\t\t\t\tif(b2 == true)\n\t\t\t\t\t\tas++;\n\t\t\t\t}\n\t\t\t\telse if(t == 2)\n\t\t\t\t{\n\t\t\t\t\tbool b1 = bob.unionBySize(u, v);\n\t\t\t\t\tif(b1 == false)\n\t\t\t\t\t{\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t\tif(b1 == true)\n\t\t\t\t\t\tbs++;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tbool b1 = alice.unionBySize(u, v);\n\t\t\t\t\tif(b1 == false)\n\t\t\t\t\t{\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t\tif(b1 == true)\n\t\t\t\t\t\tas++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(bs != n-1 || as != n-1)\n\t\t\t\treturn -1;\n\n\t\t\treturn count;\n\t\t}\n\t};
['C++']