JAVA Solution Explained in HINDI
https://youtu.be/27uTv8PW7jw\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote 
https://youtu.be/27uTv8PW7jw\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\nSubscribe Goal:- 500\nCurrent Subscriber:- 469\n\n# Complexity\n- Time complexity: O(mlogm)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n+m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass DSU {\n    int[] parent, rank;\n\n    DSU(int n) {\n        parent = new int[n];\n        rank = new int[n];\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n            rank[i] = 0;\n        }\n    }\n\n    int find(int x) {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n\n    boolean union(int x, int y) {\n        int xRoot = find(x);\n        int yRoot = find(y);\n        if (xRoot != yRoot) {\n            if (rank[xRoot] < rank[yRoot]) {\n                parent[xRoot] = yRoot;\n            } else if (rank[xRoot] > rank[yRoot]) {\n                parent[yRoot] = xRoot;\n            } else {\n                parent[yRoot] = xRoot;\n                rank[xRoot]++;\n            }\n            return true;\n        }\n        return false;\n    }\n}\n\nclass Solution {\n    public int maxNumEdgesToRemove(int n, int[][] edges) {\n        Arrays.sort(edges, (a, b) -> b[0] - a[0]);\n\n        DSU dsuAlice = new DSU(n + 1);\n        DSU dsuBob = new DSU(n + 1);\n\n        int removedEdges = 0, aliceEdges = 0, bobEdges = 0;\n\n        for (int[] edge : edges) {\n            if (edge[0] == 3) {\n                if (dsuAlice.union(edge[1], edge[2])) {\n                    dsuBob.union(edge[1], edge[2]);\n                    aliceEdges++;\n                    bobEdges++;\n                } else {\n                    removedEdges++;\n                }\n            } else if (edge[0] == 2) {\n                if (dsuBob.union(edge[1], edge[2])) {\n                    bobEdges++;\n                } else {\n                    removedEdges++;\n                }\n            } else {\n                if (dsuAlice.union(edge[1], edge[2])) {\n                    aliceEdges++;\n                } else {\n                    removedEdges++;\n                }\n            }\n        }\n\n        if (aliceEdges == n - 1 && bobEdges == n - 1) {\n            return removedEdges;\n        } else {\n            return -1;\n        }\n    }\n}\n```
['Java']