Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Graph

Reorder Routes to Make All Paths Lead to the City Zero

ID:1466

PreviousAll Paths from Source to TargetNextMax Points on a Line

Last updated 3 years ago

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.

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).

Idea

Queue + BFS + Direction Reverse

Treat the graph as undirected. Start a dfs/bfs from the root, if you come across an edge in the forward direction, you need to reverse the edge.

Start from 0, find all undirected neighbors from 0: 1, 4, and check if there's a link between 0->1 or 0->4 in directed graph. Here we see that 0->1 is in directed graph, so 0->1 need to be reversed because all edges should be coming toward 0, instead of away from 0

Code

 public int minReorder(int n, int[][] connections) {
    Map<Integer, List<Integer>> undirected = new HashMap<>();
    Map<Integer, List<Integer>> directed = new HashMap<>();
        
    for(int[] c: connections){
        int from = c[0];
        int to = c[1];
        List<Integer> nei = directed.getOrDefault(from, new ArrayList<>());
        nei.add(to);
        directed.put(from, nei);
        List<Integer> listFrom = undirected.getOrDefault(from, new ArrayList<>());
        listFrom.add(to);
        undirected.put(from, listFrom);
        List<Integer> listTo = undirected.getOrDefault(to, new ArrayList<>());
        listTo.add(from);
        undirected.put(to, listTo); 
    }
    Queue<Integer> q = new LinkedList<>();
    Set<Integer> seen = new HashSet<>();
    seen.add(0);
    q.offer(0);
        
    int rev = 0;
    while(!q.isEmpty()){
        int curr = q.poll();
        for(int nei: undirected.get(curr)){
            if(!seen.contains(nei)){
                if(directed.get(curr) !=null && directed.get(curr).contains(nei)){
                    rev++;
                }
                q.offer(nei);
                seen.add(nei);
            }
        }
    }
    return rev;
}