1
class Solution {
2
int dfs(List<List<Integer>> al, boolean[] visited, int from) {
3
int change = 0;
4
visited[from] = true;
5
for (var to : al.get(from))
6
if (!visited[Math.abs(to)]) change += dfs(al, visited, Math.abs(to)) + (to > 0 ? 1 : 0);
7
return change;
8
}
9

10
public int minReorder(int n, int[][] connections) {
11
List<List<Integer>> al = new ArrayList<>();
12
for (int i = 0; i < n; ++i) al.add(new ArrayList<>());
13
for (var c : connections) {
14
al.get(c[0]).add(c[1]);
15
al.get(c[1]).add(-c[0]);
16
}
17
return dfs(al, new boolean[n], 0);
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0