1class Solution {2int dfs(List<List<Integer>> al, boolean[] visited, int from) {3int change = 0;4visited[from] = true;5for (var to : al.get(from))6if (!visited[Math.abs(to)]) change += dfs(al, visited, Math.abs(to)) + (to > 0 ? 1 : 0);7return change;8}910public int minReorder(int n, int[][] connections) {11List<List<Integer>> al = new ArrayList<>();12for (int i = 0; i < n; ++i) al.add(new ArrayList<>());13for (var c : connections) {14al.get(c[0]).add(c[1]);15al.get(c[1]).add(-c[0]);16}17return dfs(al, new boolean[n], 0);18}19}