2023-04-30 Daily Challenge

Today I have done leetcode's April LeetCoding Challenge with cpp.

April LeetCoding Challenge 30

Description

Remove Max Number of Edges to Keep Graph Fully Traversable

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

  • Type 1: Can be traversed by Alice only.
  • Type 2: Can be traversed by Bob only.
  • Type 3: Can be traversed by both Alice and Bob.

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 Alice and Bob cannot fully traverse the graph.

 

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:

  • 1 <= n <= 105
  • 1 <= edges.length <= min(105, 3 * n * (n - 1) / 2)
  • edges[i].length == 3
  • 1 <= typei <= 3
  • 1 <= ui < vi <= n
  • All tuples (typei, ui, vi) are distinct.

Solution

auto speedup = [](){
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  return 0;
}();
struct UnionSet {
  vector<int> parent;
public:
  UnionSet(int size): parent(size) {
    for(int i = 0; i < size; ++i) {
      parent[i] = i;
    }
  }

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

  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    parent[x] = y;
  }
};
class Solution {
  bool addEdge(UnionSet &graph, const vector<int> &edge) {
    if(graph.find(edge[1] - 1) != graph.find(edge[2] - 1)) {
      graph.merge(edge[2] - 1, edge[1] - 1);
      return true;
    }
    return false;
  }
public:
  int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
    UnionSet alice(n), bob(n);
    sort(edges.begin(), edges.end(), greater<vector<int>>());

    int answer = 0;
    for(const auto &edge : edges) {
      if(edge[0] == 3) {
        bool result = !addEdge(alice, edge);
        result = !addEdge(bob, edge) && result;
        answer += result;
      } else if(edge[0] == 2) {
        answer += !addEdge(bob, edge);
      } else {
        answer += !addEdge(alice, edge);
      }
      // cout << edge << ' ' << answer << endl;
    }
    int ag0 = alice.find(0);
    int bg0 = bob.find(0);
    for(int i = 1; i < n; ++i) {
      // cout << ag0 << ' '<< alice.find(i) << endl;
      // cout << bg0 << ' ' << bob.find(i) << endl;
      if(alice.find(i) != ag0) return -1;
      if(bob.find(i) != bg0) return -1;
    }

    return answer;
  }
};

// Accepted
// 85/85 cases passed (868 ms)
// Your runtime beats 16.67 % of cpp submissions
// Your memory usage beats 50.72 % of cpp submissions (141 MB)