Remove Max Number of Edges to Keep Graph Fully Traversable

Alice and Bob have an undirected graph of n nodes and 3 types of edges:

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.

 

Example 1:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.

Example 2:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.

Example 3:

Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.
 

 

Constraints:


Solution:

class Solution {
   
    static class UF {
        int[] parent;

        public UF(int n) {
            parent = new int[n];
            for (int i = 1; i < n; i++) {
                parent[i] = i;
            }
        }

        public boolean union(int x, int y) {
            int px = find(x);
            int py = find(y);
            if (px == py) {
                return false;
            }
            parent[px] = py;
            return true;
        }

        private int find(int x) {
            return parent[x] = x == parent[x] ? x : find(parent[x]);
        }
    }

    public int maxNumEdgesToRemove(int n, int[][] edges) {
        int cnt1 = 0;
        int cnt2 = 0;
        int res = 0;
        UF uf = new UF(n + 1);
        for (int[] edge : edges) {
            if (edge[0] == 3) {
                if (uf.union(edge[1], edge[2])) {
                    cnt1++;
                    cnt2++;
                }
                else {
                    res++;
                }
            }
        }
        int[] parentcpy = uf.parent.clone();
        for (int[] edge : edges) {
            if (edge[0] == 1) {
                if (uf.union(edge[1], edge[2])) {
                    cnt1++;
                }
                else {
                    res++;
                }
            }
        }
        uf.parent = parentcpy;
        for (int[] edge : edges) {
            if (edge[0] == 2) {
                if (uf.union(edge[1], edge[2])) {
                    cnt2++;
                }
                else {
                    res++;
                }
            }
        }
        return cnt1 == n - 1 && cnt2 == n - 1 ? res : -1;
    }
}