1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution

In this guide, you will get 1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution with the best time and space complexity. The solution to Reorder Routes to Make All Paths Lead to the City Zero problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Reorder Routes to Make All Paths Lead to the City Zero solution in C++
  4. Reorder Routes to Make All Paths Lead to the City Zero solution in Java
  5. Reorder Routes to Make All Paths Lead to the City Zero solution in Python
  6. Additional Resources
1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution image

Problem Statement of Reorder Routes to Make All Paths Lead to the City Zero

There are n cities numbered from 0 to n – 1 and n – 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.
Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.
This year, there will be a big event in the capital (city 0), and many people want to travel to this city.
Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.
It’s guaranteed that each city can reach city 0 after reorder.

See also  1748. Sum of Unique Elements LeetCode Solution

Example 1:

Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
Output: 3
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).

Example 2:

Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
Output: 2
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).

Example 3:

Input: n = 3, connections = [[1,0],[2,0]]
Output: 0

Constraints:

2 <= n <= 5 * 104
connections.length == n – 1
connections[i].length == 2
0 <= ai, bi <= n – 1
ai != bi

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution in C++

class Solution {
 public:
  int minReorder(int n, vector<vector<int>>& connections) {
    vector<vector<int>> graph(n);

    for (const vector<int>& connection : connections) {
      const int u = connection[0];
      const int v = connection[1];
      graph[u].push_back(v);
      graph[v].push_back(-u);  // - := u -> v
    }

    return dfs(graph, 0, -1);
  }

 private:
  int dfs(const vector<vector<int>>& graph, int u, int prev) {
    int change = 0;

    for (const int v : graph[u]) {
      if (abs(v) == prev)
        continue;
      if (v > 0)
        ++change;
      change += dfs(graph, abs(v), u);
    }

    return change;
  }
};
/* code provided by PROGIEZ */

1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution in Java

class Solution {
  public int minReorder(int n, int[][] connections) {
    List<Integer>[] graph = new List[n];

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] connection : connections) {
      final int u = connection[0];
      final int v = connection[1];
      graph[u].add(v);
      graph[v].add(-u); // - := u -> v
    }

    return dfs(graph, 0, -1);
  }

  private int dfs(List<Integer>[] graph, int u, int prev) {
    int change = 0;

    for (final int v : graph[u]) {
      if (Math.abs(v) == prev)
        continue;
      if (v > 0)
        ++change;
      change += dfs(graph, Math.abs(v), u);
    }

    return change;
  }
}
// code provided by PROGIEZ

1466. Reorder Routes to Make All Paths Lead to the City Zero LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  2151. Maximum Good People Based on Statements LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.