Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 1579: Remove Max Number of Edges to Keep Graph Fully Traversable

Union FindGraph

LeetCode 1579 Solution Explanation

Explanation:

  • We need to find the maximum number of edges we can remove so that the graph can still be fully traversed by both Alice and Bob.
  • We can achieve this by simulating the process of building the graph by Alice and Bob separately and counting the number of edges that can be removed without affecting the full traversal by both.
  • We will use Disjoint Set Union (DSU) data structure to keep track of the connected components for Alice and Bob separately.
  • We will iterate through the edges and process type 3 edges first, then process type 1 and type 2 edges separately for Alice and Bob.
  • If at any point both Alice and Bob can reach all nodes, we can remove that edge. If Alice or Bob cannot reach all nodes, we can't remove that edge.
  • Finally, we return the maximum number of edges that can be removed.

:

LeetCode 1579 Solutions in Java, C++, Python

class Solution {
    public int maxNumEdgesToRemove(int n, int[][] edges) {
        DSU alice = new DSU(n);
        DSU bob = new DSU(n);
        int removedEdges = 0;

        // Process Type 3 edges
        for (int[] edge : edges) {
            if (edge[0] == 3) {
                if (!alice.union(edge[1], edge[2]) || !bob.union(edge[1], edge[2])) {
                    removedEdges++;
                }
            }
        }

        // Process Type 1 edges for Alice
        for (int[] edge : edges) {
            if (edge[0] == 1) {
                if (!alice.union(edge[1], edge[2])) {
                    removedEdges++;
                }
            }
        }

        // Process Type 2 edges for Bob
        for (int[] edge : edges) {
            if (edge[0] == 2) {
                if (!bob.union(edge[1], edge[2])) {
                    removedEdges++;
                }
            }
        }

        return alice.isConnected() && bob.isConnected() ? removedEdges : -1;
    }

    class DSU {
        int[] parent;

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

        public int find(int x) {
            if (parent[x] != x) {
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

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

        public boolean isConnected() {
            int root = find(1);
            for (int i = 2; i < parent.length; i++) {
                if (find(i) != root) {
                    return false;
                }
            }
            return true;
        }
    }
}

Interactive Code Editor for LeetCode 1579

Improve Your LeetCode 1579 Solution

Use the editor below to refine the provided solution for LeetCode 1579. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems