package leetcode;

/**
 * https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/
 */
public class Problem1466 {
    public int minReorder(int n, int[][] connections) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1466 prob = new Problem1466();
        System.out.println(prob.minReorder(
            6, new int[][]{{0,1},{1,3},{2,3},{4,0},{4,5}})); // 3
        System.out.println(prob.minReorder(
            5, new int[][]{{1,0},{1,2},{3,2},{3,4}})); // 2
        System.out.println(prob.minReorder(
            3, new int[][]{{1,0},{2,0}})); // 0
    }
}

@@ -12,16 +12,116 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -304,129 +304,414 @@
     
-// TODO%0A        return 0;
+List%3CInteger%3E%5B%5D graph = new List%5Bn%5D;%0A        for (int i = 0; i %3C n; i++) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1466 prob = new Problem1466(
+    graph%5Bi%5D = new ArrayList%3C%3E();%0A        %7D%0A        Set%5B%5D direction = new Set%5Bn%5D;%0A        for (int i = 0; i %3C n; i++) %7B%0A            direction%5Bi%5D = new HashSet();%0A        %7D%0A        for (int%5B%5D connection : connections) %7B%0A            int from = connection%5B0%5D;%0A            int to = connection%5B1%5D;%0A            graph%5Bfrom%5D.add(to
 );%0A 
@@ -721,43 +721,69 @@
     
+ 
-System.out.println(prob.minReorder(
+   graph%5Bto%5D.add(from);%0A            direction%5Bfrom%5D.add(to);
 %0A   
@@ -791,261 +791,550 @@
     
+%7D%0A
     
-6, new int%5B%5D%5B%5D%7B%7B0,1%7D,%7B1,3%7D,%7B2,3%7D,%7B4,0%7D,%7B4,5%7D%7D)); // 3
+    return dfs(graph, new boolean%5Bn%5D, direction, 0);
 %0A    
+%7D%0A%0A
     
-System.out.println(prob.minReorder(%0A            5, new int%5B%5D%5B%5D%7B%7B1,0%7D,%7B1,2%7D,%7B3,2%7D,%7B3,4%7D%7D)); // 2%0A        System.out.println(prob.minReorder(%0A            3, new int%5B%5D%5B%5D%7B%7B1,0%7D,%7B2,0%7D%7D)); // 0
+private static int dfs(List%3CInteger%3E%5B%5D graph, boolean%5B%5D visited,%0A                           Set%5B%5D direction, int node) %7B%0A        int count = 0;%0A        visited%5Bnode%5D = true;%0A        for (int neighbor : graph%5Bnode%5D) %7B%0A            if (!visited%5Bneighbor%5D) %7B%0A                if (direction%5Bnode%5D.contains(neighbor)) %7B%0A                    count++;%0A                %7D%0A                count += dfs(graph, visited, direction, neighbor);%0A            %7D%0A        %7D%0A        return count;
 %0A   

Solve problem 1466